Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

I am still failing the tests 3 & 4 and I am at a loss on why it is wrong. Is the instructions asking for a specific method that I missed ? My output on the console is correct. I am using an “asterisk” as a “space” placeholder to visually see the spaces in the pyramid, and I remove the “asterisk” and place a " " and I delete my log statements when I run my tests. Please, if I can receive a clue on why my function is not passing, I would be grateful! Thank you!

Your code so far

function pyramid(str, num, boo) {
  let char = "";
  let spaceChar = "*";
  // "*" is a visual symbol to see the spaces.
  if (boo === false) {
    for (let i = 0; i < num; ++i) {
      let numSpace = num - 1;
      let bigSpace = spaceChar.repeat(numSpace - i) + str.repeat(i + 1);
       char += "\n" + bigSpace + str.repeat(i)+ "\n";
      
    }
  }
  if (boo === true) {
    for (let i = num; i > 0; i--) {
      let bigSpace = spaceChar.repeat(num - i) + str.repeat(i - 1);
       char += "\n" + bigSpace + str.repeat(i)+ "\n";
       }
  }
return char;
}

console.log("This pyramid is upwards (false):" + "\n" + pyramid("o", 4, false));

console.log("This pyramid is downwards (true):" + "\n" + pyramid("p", 5, true));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Try wrapping the function return getting logged inside a JSON.stringify call to see the characters (and stop using *).

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

Thank you, I was able to see there was an extra “new line” that was being printed with every loop. (the * was used as a visual to see how many spaces were being printed in the log, its hard to count the spaces when I can’t see them lol :sweat_smile: . I did replace the * with a “space” when I preformed the tests )

1 Like