steps(2)
'# '
'##'
steps(3)
'# '
'## '
'###'
steps(4)
'# '
'## '
'### '
'####'
My Solution
function steps(n) {
if (n === 0) {
return;
}
for (let row = 1; row <= n; row++) {
console.log(''.padStart(row, '#').padEnd(n, ' '));
}
}
SG Solution 1
function steps(n, row = 0, stair = '') {
if (n === row) {
return;
}
if (n === stair.length) {
console.log(stair);
return steps(n, row + 1);
}
const char = stair.length <= row ? '#' : ' ';
steps(n, row, stair + char)
}
SG Solution 2
function steps(n) {
for (let row = 0; row < n; row++) {
let stair = '';
for (let column = 0; column < n; column++) {
if (column <= row) {
stair += '#';
} else {
stair += ' ';
}
}
console.log(stair);
}
}