Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

I’m trying to complete the pyramid generator but the last 2 tests are not passing even though I am getting the correct output on the console. Please assist and advise what am I missing.

Your code so far

function pyramid(char, rows, isUp) {
  const maxLength = 2 * rows - 1;
  let pattern = "\n";
  if (isUp == false) {
    for (let i = 1; i <= rows; i++) {
      let repeatNum = Math.floor((maxLength - (2 * i - 1)) / 2);
      pattern += " ".repeat(repeatNum) + char.repeat(2 * i - 1) + " ".repeat(repeatNum) + "\n";
    }
  }
  else if (isUp == true) {
    for (let i = rows; i >=1; i--) {
      let repeatNum = Math.floor((maxLength - (2 * i - 1)) / 2);
      pattern += " ".repeat(repeatNum) + char.repeat(2 * i - 1) + " ".repeat(repeatNum) + "\n";
    }
  }

  return pattern;
}

console.log(pyramid("o", 4, false))

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

Hi @Myaluza

Your pyramid has spaces on the upper levels.
Compare with the expected output.

Happy coding

1 Like

Thank you, that worked

1 Like