CodeWars 코딩 문제 2021/01/20 - Xmas Tree

이호현·2021년 1월 20일
0

Algorithm

목록 보기
56/138

[문제]

Complete the function that returns a christmas tree of the given height. The height is passed through to the function and the function should return a list containing each line of the tree.

XMasTree(5) should return : ['__#__', '###', '#####', '#######', '#########', '__#__', '__#__']
XMasTree(3) should return : ['#', '###', '#####', '#', '#']

The final idea is for the tree to look like this if you decide to print each element of the list:

Pad with underscores i.e _ so each line is the same length. The last line forming the tree having only hashtags, no spaces. Also remember the trunk/stem of the tree.

(요약) 크리스마트 트리 만들기

[풀이]

function xMasTree(n){
  let answer = [];

  for(let i = 1; i <= n; i++) {
    answer.push('#'.repeat(i).padStart(n, '_') + '#'.repeat(i - 1).padEnd(n - 1, '_'));
  }

  answer.push('#'.padStart(n, '_').padEnd((2 * n) - 1, '_'));
  answer.push('#'.padStart(n, '_').padEnd((2 * n) - 1, '_'));

  return answer;
}

지난번에 풀었던 christmax tree의 재탕인데 나무 줄기 만드는거 추가.

profile
평생 개발자로 살고싶습니다

0개의 댓글