Solve the HackerRank Staircase Problem in JavaScript

Waqas Ahmad
2 min readJan 31, 2021

If you have studied for software development interviews, then I am sure you have, or will eventually run into the “Staircase” problem where…

  • n is an integer denoting the size of the staircase
  • print a staircase of size n using # symbols and spaces and make it right-aligned
  • Example: n = 4 should print:

First I do some paper work to define the relation between space character and # symbol as paper work is good to save time in programming i always use this approach before to write any single line of code.Here is my paper work but i don`t have clear yet to its implementation I think need a two for loop to achieve this thing.

Pattren to draw StairCase

when I discovered Javascript’s String.prototype.repeat().

Its become easy to resolve like a magic no need to inner loop just use the repeat() Method.

I am no wizard though…it took some time for me to understand how to use .repeat() before I was able to pass the tests on HackerRank again. But it was a great lesson on how to read documentation and then apply it.

I look forward to using .repeat() in more ways!

--

--

Waqas Ahmad

Software Engineering Leader | Consultant Cloud Infra DBA at Systems Limited