Steps Function Algo

Dolly Desir
3 min readApr 11, 2021

Hello again ! Since diving into algorithms I’ve done several that involved searching for something specific or returning something. Last week though I came across an algorithm in which you would write a function that accepts a positive number. The function should console log a sequence in the pattern of steps with n levels using the # symbol. Make sure the step has spaces on the right hand side.

this is the expected output when n = 5.

As I do with all problems, I restate it in my own words to make sure I understand what is expected of me. I think about what is the input, but for this problem it was easy enough to know that my input is a positive number. What are the outputs? It’s expecting a hashtag but how do I account for the blank spaces?

solution.

One of the things I always do when I look at a solution is trying to not only understand what’s happening but can I explain it? On line 56, row = 0 is the index point from which the looping starts, same for line 59.

I need to count down through row but also count across through columns.

Looking back at the solution, I really didn’t understand a specific block of code because I was thinking about it differently. What I had to realize this conditional was just checking the indexes not if anything was in the index at the time of the loop.

It’s a loop within a loop, usually frowned upon I’ve been told but for this function the way the conditionals work while in the row loop, we need to check all the indexes in the column loop before breaking out and moving onto the next row index.

The variable stair = ‘ ‘ is just an empty string until we add to it depending on what conditional is met. One of the things that was confusing was line 61, if (column <= row){}, remember we are just comparing the indexes. For instance row[0] is less than column[5] and the code is saying if column is less than row add a #, if not add an empty space. So during row’s loop it’s checking column’s indexes against the current row index that it is at currently. When the first row loop runs, it’s at row[0], then going into the column loop, first one being column[0], row[0] then checks column[0] then runs the conditional against column[0] and will do this check against each column up until n which in this case is 5. Row[0]’s loop is done then row[1] does the same exact thing.

I hope I explained clear enough to not be confusing but I think the screenshots help as you can see a visual explanation of what’s happening in both loops. Happy Coding !!

--

--