Algorithms

Staircase

seoca 2020. 8. 4. 20:00

 

Problem

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.

 

 

Solution

function staircase(n) {
for (let i = 1; i <= n; i++) {
        console.log("#".repeat(i).padStart(n));
    }
}

 

Result

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

 

Reference

https://www.hackerrank.com/challenges/staircase/problem

 

Staircase | HackerRank

Print a right-aligned staircase with n steps.

www.hackerrank.com