JS Algorithm – Steps

Giant’s Causeway, Bushmills, United Kingdom
Write a function that accepts a 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!
Examples
steps(2)
‘# ‘
‘##’
steps(3)
‘# ‘
‘## ‘
‘###’
steps(4)
‘# ‘
‘## ‘
‘### ‘
‘####’
First Solution
function steps(n) {
// Go through every row
for (let row = 0; row < n; row++) {
// stair variable declaration in every
// cycle the row
let stair = '';
// Go through every column (of each row)
for (column = 0; column < n; column++) {
// codition to insert or not the #
if (column <= row) {
stair += '#';
} else {
stair += ' ';
}
}
// After the loop of column finish
// console the stair that contains the string
console.log(stair);
}
}
steps(3);
Result:

Second solution
// Function declaration with
// needed parameters
function steps(n, row = 0, stair = '') {
// If n is equal to 0 then the program end
if (n === row) {
return;
}
// If n is equal to stair (string) length
// this will mean that we need to get the next row
if (n === stair.length) {
// print the stair string
console.log(stair);
// Call the same function again
// go through the next row
return steps(n, row + 1);
}
// If stair (string) length is less than row
if (stair.length <= row) {
// then add # to stair (string)
stair += '#';
} else {
// else add empty space to stair (string)
stair += ' ';
}
// Call the same function again
steps(n, row, stair);
}
steps(3);
Result:

By Cristina Rojas