Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

My code is not passing and I’m unclear as to why. I thought it was because of the way i added “\n”, but it does not appear to make a difference at the beginning or end of where I’m generating my pyramid variable within the loop. Is it something to do with how I’m using the counter? Thanks in advance!

Your code so far

const pyramid = (character, rows, inverted) => {
  let pyramid = '';

  if (inverted) {
    console.log("I'm true!")
    for (let i = rows; i >= 1; i--) {
      const chars = character.repeat(2 * i-1);
      const spaces = ' '.repeat(rows - i)
      // console.log('chars:', chars, "spaces:", spaces)
      pyramid += '\n' + spaces + chars + spaces
    }
  } else {
    console.log("I'm false!")
    for (let i = 1; i <= rows; i++) {
      const chars = character.repeat(2 * i -1);
      const spaces = ' '.repeat(rows - i);
      // console.log("spaces:", spaces)
      // console.log("numChars", numChars)
      pyramid += '\n' + spaces + chars + spaces
    }
  }
  return pyramid;
};


console.log(pyramid('p', 5, true))




Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.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

You can add this code to the bottom of your script file to compare what the tests expect to what your code is producing:

  console.log(pyramid("o", 4, false));
  console.log(pyramid("p", 5, true));
  
  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"));

Now I see what I was missing! Thank you!