Build Tower ( 6 kyu )

문제

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

for example, a tower of 3 floors looks like below

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

주어진 숫자만큼의 별탑을 쌓는 문제

풀이

function towerBuilder(nFloors) {
  let result = [];
  for ( let i = 1; i <= nFloors; i++ ) {
    result.push(' '.repeat(nFloors-i) + '*'.repeat(i*2-1) + ' '.repeat(nFloors-i))
  }
  return result
}

repeat메소드로 쉽게 해결

다른 사람의 풀이

function towerBuilder(n) {
  return Array.from({length: n}, function(v, k) {
    const spaces = ' '.repeat(n - k - 1);
    return spaces + '*'.repeat(k + k + 1) + spaces;
  });
}

Array.from 대체 무엇


+ Recent posts