Tell us what’s happening:
I’m not sure what’s going on here because the function is working and outputting what it should, though it shows this: “\n o\n ooo\n ooooo\nooooooo\n”. Does this have to do with the spaces or what exactly? Thanks!
Your code so far
function pyramid(strPattern, rowsNum, boolVal) {
let num = 1;
let pyramidStr = "";
let pyramidSpace = rowsNum;
if (boolVal === false) {
for (let row = 0; row < rowsNum; row++) {
if (row === 0) {
pyramidStr += `\n${" ".repeat(pyramidSpace)}${strPattern}${" ".repeat(pyramidSpace)}\n`;
} else {
num += 2;
pyramidSpace -= 1;
pyramidStr = pyramidStr + " ".repeat(pyramidSpace) + strPattern.repeat(num) + " ".repeat(pyramidSpace) + "\n";
}
}
} else {
for (let rowNum = 1; rowNum < rowsNum; rowNum++) {
num += 2;
pyramidSpace -= 1;
}
for (let row = 0; row < rowsNum; row++) {
if (row === 0) {
pyramidStr += `\n${" ".repeat(pyramidSpace)}${strPattern.repeat(num)}${" ".repeat(pyramidSpace)}\n`;
} else {
num -= 2;
pyramidSpace += 1;
pyramidStr = pyramidStr + " ".repeat(pyramidSpace) + strPattern.repeat(num) + " ".repeat(pyramidSpace) + "\n";
}
}
}
return pyramidStr;
}
console.log(pyramid("o", 5, false));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Challenge Information:
Build a Pyramid Generator - Build a Pyramid Generator