Algorithms and Data Structures Part VIII

Brian Butterly
3 min readApr 2, 2021

Steps

Photo by Rodion Kutsaev on Unsplash

Okay so this problem is a good one and my next post after this is very closely related to this.

Let's start with some directions.

We are asked to write a function that accepts a positive number N. The function should console log a step shape with N levels using the # character. We are also asked to make sure the step has spaces on the right hand side.

e.g.

steps(2)
'# '
'##'
steps(3)
'# '
'## '
'###'

and so on. Notice the empty spaces, this is important to remember.

Once again we start with our function…

First we will iterate from 0 to n in the current row we’re operating on…

Now for every different row we need to create a brand new, empty string…

Now we need to iterate the same way but for all the columns.

now we want to say if the current column is less than or equal to the current row then add a # to the stair string…

Now if column is not less than or equal to row we will add a space instead of the # symbol…

don’t forget the space in the “” you’re not creating an empty string so you need the space “ “.

Finally, after processing one entire row and have a full stair string we just console log stair…

Make sure the console log of stair is still inside the row, for loop, because we want to end up with N number of console logs. N being 1,2,3,4 etc, how many number of stairs you pass as an argument.

That's the full code for this problem and we will be using some of this in the next blog post, ‘two sided steps’ or ‘pyramids’.

Thanks for reading!

--

--