Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

Please help me, I don’t know how to think of a logic to build this pyramid with correct number of rows, also spacing is troubling me.

Your code so far

const pyramid = (character, rows, boolean) => {
  let space =" ";
  if(boolean === false) {
    for(let i = 1; i <= rows; i+= 2) {
      console.log(space.repeat(rows-i) + i.toString().replace(i, character).repeat(i));
    }
  } else {
    for(let i = rows; i >= 1; i-= 2) {
      console.log(space.repeat(rows-i) + i.toString().replace(i, character).repeat(i));
    }
  }
}

Your browser information:

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

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Hi @mr-aakash

Your pyramid is:

  • upside-down
  • not centered
  • has an undefined level

If you want, you can visit the Pyramid Generator practice project.
But first, write down in words what you want to happen.
Then write code that accomplishes that.

Happy coding

I can do that but won’t that be considered like cheating ?

I have completed all topics required to build this project but can’t write the logic for this project on my own.

You can and should review code you wrote, especially when learning.
However, do not copy and paste code as you will then not learn anything.

Sometimes you will get stuck, however the forum is here to help.

How would you describe the first part of the second pyramid from the image above?

Okay then I will first do the pyramid generator workshop then complete this lab project on my own.

In my loop when in inrement/ decrement i write, i++ then that undefined rows doesn’t appear but for pyramid I need odd rows, then I get less no. of rows in the console.

for e.g. see this code-

const pyramid = (character, rows, boolean) => {
  let space =" ";
  if(boolean === false) {
    for(let i = 1; i <= rows; i+= 1) {
      console.log(i.toString().replace(i, character).repeat(i));
    }
  } else {
    for(let i = rows; i >= 1; i-= 1) {
      console.log(i.toString().replace(i, character).repeat(i));
    }
  }
}

Hi. Your pyramid function is correctly structured, but it lacks spaces to align the characters into a pyramid shape. Right now, the characters will just stack in a left-aligned format, not forming an actual pyramid. You need to add spaces before each row to center-align the characters.

Looking a lot better.

As @hasanzaib1389 mentioned, you need to add spaces to center the character variable.

Also, try and work out where the undefined is coming from.

I can see something else that is not right with the second level, you are right, it has something to do with the incrementor.

sorry I was sick then I had one exam so I was inactive and unable to reply.

my corrected my logic, but now when I console.log my logic inside for loop I get a correct pyramid but when I return, then I only get the last row.

function pyramid(character, rows, isInverted) {
  if(isInverted === false) {
    for(let i = 1; i <= rows; i++) {
      return " ".repeat(rows-i) + character.repeat(2 * i -1) + " ".repeat(rows-i);
    }
  }
  else {
    for(let i = rows; i >= 1; i--) {
      return " ".repeat(rows-i) + character.repeat(2 * i -1) + " ".repeat(rows-i);
  }
}
}

sorry I was sick then I had one exam so I was inactive and unable to reply.

I modified the logic and it works properly now. But now facing another issue, please see my above reply.

issue in your code is that you are using return inside the loop, which causes the function to exit after the first iteration instead of generating a full pyramid. Additionally, the output needs to be collected in a variable and joined properly before returning. The extra spaces at the end of each line are unnecessary and can be removed.

function pyramid(character, rows, isInverted) {
  let level = "";
  if(isInverted === false) {
    for(let i = 1; i <= rows; i++) {
      level = " ".repeat(rows-i) + character.repeat(2 * i -1) + "\n";
    }
    return level;
  }
  else {
    for(let i = rows; i >= 1; i--) {
      return " ".repeat(rows-i) + character.repeat(2 * i -1) + " ".repeat(rows-i);
  }
}
}

still not working, how to fix it ?

I did as you said, only added new line code, still same result as earlier

you aren’t removed spaces in the end in above statement. That’s also should not be return.

function pyramid(character, rows, isInverted) {
  let level = "";
  if(isInverted === false) {
    for(let i = 1; i <= rows; i++) {
      level = " ".repeat(rows-i) + character.repeat(2 * i -1) + "\n";
    }
    return level;
  }
  else {
    for(let i = rows; i >= 1; i--) {
      level = " ".repeat(rows-i) + character.repeat(2 * i -1) + "\n";
  }
  return level;
}
}

Done but still not getting desired result.

return statement should be only in the end of the function

function pyramid(character, rows, isInverted) {
  let level = "";
  if(isInverted === false) {
    for(let i = 1; i <= rows; i++) {
      level = " ".repeat(rows-i) + character.repeat(2 * i -1) + "\n";
    }
  }
  else {
    for(let i = rows; i >= 1; i--) {
      level = " ".repeat(rows-i) + character.repeat(2 * i -1) + "\n";
  }
} return level;
}

still same as previous one :frowning:

No change in outcome still.