Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

The else statement in my function doesn’t appear to be returning the expected result, for some reason my pyramid variable isn’t being updated in the loop. It works in my loop within the if statement despite the code within the two not being that different. Any help greatly appreciated!

Your code so far

function pyramid(char, rows, vertex) {
  let space = " ";
  let pyramid = "\n";
  let counter = 1;
  let y = 2;
  if (vertex == false) {
    for (let i = 1; i < rows + 1; i++) {
      let spaces = space.repeat(rows - i);
      let characters = char.repeat(counter);
      pyramid = pyramid.concat(spaces, characters, "\n");
      counter += y;
    };
    return pyramid;
  }
  else if (vertex == true) {
    for (let i = rows; i < 1; i--) {
      let spaces = space.repeat(rows - i);
      let characters = char.repeat((rows * 2) - counter);
      pyramid = pyramid.concat(spaces, characters, "\n");
      counter += y;
    };
    return pyramid;
  };
};

console.log(pyramid("o", 4, true));
//expected first loop "\nooooooo\n"
//expected second loop "\nooooooo\n ooooo\n"

Your browser information:

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

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Your problem is here. When will this loop run?

1 Like

Ahh I see, I overlooked the condition, it doesn’t run because i < 1 is false. Thank you!

1 Like