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!
steps(2)
'# '
'##'
steps(3)
'# '
'## '
'###'
steps(4)
'# '
'## '
'### '
'####'
Two solutions:-
1) Iterative (recommended)
2) Recursive
function steps(n) {
for(let row=0; row<n; row++){
let stair = "";
for (let col=0; col<n; col++){
if (col <= row){
stair += "#";
} else
stair += " ";
}
console.log(stair);
}
}
function steps(n, row = 0, stair = "") {
if (n === row){
return;
}
if (n === stair.length){ // at the end of the row
console.log(stair);
return steps(n, row + 1);
}
const add = stair.length <= row? "#" : " "; //ternary operator
steps(n, row, stair + add);
}
steps(2)
steps(3)
steps(4)