N
.N
levels using the #
character.pyramid(1)
'#'
pyramid(2)
' # '
'###'
pyramid(3)
' # '
' ### '
'#####'
My Solution
function pyramid(N) {
if (N === 0) {
return;
}
const calcLevelSize = (n) => n * 2 - 1
const pyramidBase = calcLevelSize(N);
let level = 0;
let leftPadding = 0;
for (let row = 1; row <= N; row++) {
level = calcLevelSize(row);
if (level < pyramidBase) {
leftPadding = (pyramidBase - level) / 2
console.log(`"${''.padStart(leftPadding, ' ').padEnd(leftPadding + level, '#').padEnd(pyramidBase, ' ')}"`);
} else {
console.log(`"${''.padStart(level, '#')}"`);
}
}
}
SG Solution 1
function pyramid(N, row = 0, level = '') {
if (N === row) {
return;
}
if (level.length === 2 * N - 1) {
console.log(level);
return pyramid(N, row + 1);
}
const midpoint = Math.floor((2 * N - 1) / 2);
const char = midpoint - row <= level.length && midpoint + row >= level.length ? '#' : ' ';
pyramid(N, row, level + char);
}
SG Solution 2
function pyramid(N) {
const maxLevelSize = 2 * N - 1;
const midpoint = Math.floor(maxLevelSize / 2);
for (let row = 0; row < N; row++) {
let level = '';
for (let column = 0; column < maxLevelSize; column++) {
if (midpoint - row <= column && midpoint + row >= column) {
level += '#';
} else {
level += ' ';
}
}
console.log(level);
}
}