Different approach for Staircase JavaScript problem

Tomlai
2 min readApr 30, 2021
Photo by Ricardo Gomez Angel on Unsplash

For Staircase algorithms problems, i am sure many of you have seen different way to do it. Either with high order built-in function or using more basic way of approach.

This is solution is sharing by my friend Ciara when we were working on HackerRank Staircase problem, I enjoy to work and think through algorithm problems with her. She usually have some easier understanding approach to the question. Let take a look.

Question:

Staircase detail
This is a staircase of size n = 4
...#
..##
.###
####
its base and height are both equal to n. it is a drawn using # symbols and spaces. The last line is not preceded by any spaces.
Write a program that prints a staircase of size n. (integer)

Print a staircase as described above.

As the picture showing above, we set variable for those “ “ , “#” , string, spaceCount and hashTagCount. The next step just using for loop to run through each line of the space and the hashtag. The spaceCount decrease by 1 then the hashTagCount will increasing by 1. The final step is set string equal to those the spaces.repeat() pluse hashtage.repeat(). then it will be able to console.log(string) showing the staircase.

--

--