Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

Although my code generates the two pyramids, it is not passing the tests. I cannot figure out what I am missing. Any suggestions? Thank you.

Your code so far


function pyramid(ltr, num, bool) {
  let rowPad = 0;
  let chars = "";
  let print = "";
  let e = 0;
  if (bool === false) {
    for (let i = 0; i <= num; i++) {
      rowPad = " ".repeat(num - i);
      if (e <= 1) {
        chars = ltr.repeat(e) + "\n";
        print = print + rowPad + chars;
        e++;
      } else {
        chars = ltr.repeat(e * 2 - 1) + "\n";
        print = print + rowPad + chars;
        e++;
      }
    }
  } else {
    e = num;
    for (let i = num; i >= 0; i--) {
      rowPad = " ".repeat(num - i);
      if (e <= 1) {
        chars = ltr.repeat(e) + "\n";
        print = print + rowPad + chars;
        e--;
      } else {
        chars = ltr.repeat(e * 2 - 1) + "\n";
        print = print + rowPad + chars;
        e--;
      }
    }
  }
  return print = print.toString("");
}

console.log(pyramid("o", 4, false))
console.log(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/140.0.0.0 Safari/537.36

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator
https://www.freecodecamp.org/learn/full-stack-developer/lab-pyramid-generator/lab-pyramid-generator

Take a look at the all characters returned by the function:

console.log(JSON.stringify(pyramid("o", 4, false)))
console.log(JSON.stringify(pyramid("p", 5, true)))
1 Like