[HackerRank] Staircase

Jayoung Lee·2020년 8월 28일
0

Staircase

Consider a staircase of size :

   #
  ##
 ###
####

Observe that its base and height are both equal to n, and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size n.

Function Description

Complete the staircase function in the editor below. It should print a staircase as described above.
staircase has the following parameter(s):

  • n: an integer

Input Format

A single integer, , denoting the size of the staircase.
Constraints

Output Format

Print a staircase of size n using # symbols and spaces.
Note: The last line must have 0 spaces in it.

Sample Input

6 

Sample Output

     #
    ##
   ###
  ####
 #####
######

Explanation

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of n=6.

My Solution

// Complete the staircase function below.
function staircase(n) {
    for(let i = 1; i <=n; i++){
        process.stdout.write((' ').repeat(n-i).concat(('#').repeat(i)).concat('\n'));  
    }
}

Result

(옛날 옛적에 1학년 C언어배울 때 했던 거 같은데,,,,진짜 기억 1도 안나서 슬펐음,,,뚀륵😥)
아무튼,,,, 주루룩 써놓고 규칙을 찾아보니,
띄어쓰기는 위에서부터 한 줄당 n-i개가 찍히고 i만큼 '#' 이 찍힘을 확인했다.
그래서 그걸 repeat으로 텍스트들을 붙였고, concat으로 이었다.

반복하면 반복문만 생각나서 어떻게 해야할 지 생각하다가 repeat이라는 메서드를 찾아서 했다. 문자열을 주어진 횟수만큼 반복해 붙인 새로운 문자열을 반환하는 메서드라 한다.

profile
프론트엔드 개발 공부하면서 아카이빙 중입니다 ʕ•ﻌ•ʔ

0개의 댓글