The Steps Question

The Steps Question

Write a function that accepts positive number N. The function should console log a step shape with N levels using the # character. Make sure the step has spaces on the right hand side.

Example:
steps(2)
‘# ‘
‘##’
steps(3)
‘# ‘
‘##’
‘###’
steps(4)
‘# ‘
‘## ‘
‘### ‘
‘####’




First we create function in our case steps with only one parameter named n ( n is the number of rows as described in the example above) and row and we will default the row to 0, third argument is stair we initial with empty string;

We check if (n === row) and if row we return;

Next we check if (n === stair.length) we console.log(stair)

Inside the if statement we recursivly use steps(n ,row + 1)

If we meet the above case we don’t need to do anything else so we make sure that we use the word return before the steps(n ,row + 1)

The last thing we have to do is handle the case in which we are still assembling our stair string and if the length of the stair is less than or equal to the row that we are currently working on we are going to add a ‘#’ otherwise empty space;

We check with if statement if(stair.length <= row) we return stair += ‘#’ else stair += ‘ ‘;

Then we again we use recursion calling steps(n, row, stair)

comments powered by Disqus