알고리즘 100 - Build Tower

박진현·2021년 7월 29일
0

알고리즘 (Javascript / C)

목록 보기
100/125

Q.

Build Tower
Build Tower by the following given argument:
number of floors (integer and always greater than 0).

Tower block is represented as *

Python: return a list;
JavaScript: returns an Array;
C#: returns a string[];
PHP: returns an array;
C++: returns a vector;
Haskell: returns a [String];
Ruby: returns an Array;
Lua: returns a Table;
Have fun!

for example, a tower of 3 floors looks like below

[
  '  *  ', 
  ' *** ', 
  '*****'
]

and a tower of 6 floors looks like below

[
  '     *     ', 
  '    ***    ', 
  '   *****   ', 
  '  *******  ', 
  ' ********* ', 
  '***********'
]

A)

function towerBuilder(nFloors) {
// build here
let res = [];
for (let i = 1; i <= nFloors ; i++) {
  let str = '*'.repeat((2*i)-1)
  let space = ' '.repeat(((2*nFloors)-(2*i))/2)
  res.push(space+str+space)
}
return res
}
                         
profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글