Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

Am i going crazy? It’s doing exactly what’s it’s supposed to do…

Your code so far

function pyramid (string, rows, trueorFalse) {
  let result = "";
  if (trueorFalse === false) {
    for(let i = 0; i < rows; i++) {
      let char = string.repeat(2 * i + 1);
      let space = " ".repeat(rows - i - 1);
      result += space + char + "\n";
    }
  } else {
      for(let j = rows - 1; j >= 0; j--) {
        let char = string.repeat(2 * j + 1);
        let space = " ".repeat(rows - j - 1);
        result += space + char + "\n";
      } 
    }
  return result;
}

console.log(pyramid("p", 5, true));


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Welcome to the forum @benU89 !

You can add this code to compare actual to expected:

  console.log("Actual:   ", JSON.stringify(pyramid("o", 4, false)));
  console.log("Expected: ", JSON.stringify("\n   o\n  ooo\n ooooo\nooooooo\n"));
  console.log("Actual:   ", JSON.stringify(pyramid("p", 5, true)));
  console.log("Expected: ", JSON.stringify("\nppppppppp\n ppppppp\n  ppppp\n   ppp\n    p\n"));

Happy coding!

so there should be a new line before the first line?

The expected results were taken directly from “should return” in Tests #3 and #4.

I’m confused because I thought we were building a pyramid, but it actually wants to return the string version?