Create a function christmasTree(height) or christmas_tree(height) (in ruby, python) that returns a christmas tree of the correct height
christmasTree(5) || christmas_tree(5) should return:
Height passed is always an integer between 0 and 100.
Use \n for newlines between each line.
Pad with spaces so each line is the same length. The last line having only stars, no spaces.
function christmasTree(height) { let answer = []; for(let i = 1; i <= height; i++) { answer.push('*'.repeat(i).padStart(height, ' ') + '*'.repeat(i - 1).padEnd(height - 1, ' ')); } return answer.join('\n'); }
왼쪽, 오른쪽 나눠서 문자열 계산하기로 함.
왼쪽은*
을 기준으로 왼쪽에*
을 늘려가고, 오른쪽은 빈 문자열에서*
을 오른쪽으로 하나씩 늘려감.
반복문 한번 돌때마다 이 작업을 하고, 배열에push
.
마지막에 줄바꿈 문자열\n
을 이용해join
하면 됨.