Build a Pyramid Generator - Build a Pyramid Generator

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

It is difficult to see in this situation, to see better you can use

console.log(JSON.stringify(pyramid("o", 5, false)));

which gives "\n o \n ooo \n ooooo \n ooooooo \n ooooooooo \n" in the console
now you can compare this with the expected output:

actual:  "\n     o     \n    ooo    \n   ooooo   \n  ooooooo  \n ooooooooo \n"
expected: "\n   o\n  ooo\n ooooo\nooooooo\n"

it seems you have too many spaces

Thanks! I suspected it, though I’m not completely sure why that should be an issue here.

because the string is different from the one that is expected, spaces are characters too