Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

Can someone please help. I’m just trying to figure out how to do it upright first. I know I havent included to true/false yet.

Your code so far

function pyramid(character, rows, boolean) {
  let content = "\n";
  let contentBefore = "   ";
  let contentAfter= character;
  let pyramid = new Array(rows);
  const newRow = rows*2;

for(let i=1; i<newRow; i++) {
  contentBefore += character;
 let value = contentAfter + content + contentBefore;
 pyramid[i] = value;
}

return pyramid.join("")
}

console.log(pyramid("o", 4, false))

Your browser information:

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

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

you are adding this, but the pyramid start of line is not always at the same distant from the beginning of the line

try figuring out how to make the correct number of lines in the pyramid, now you have an input of 4 but the pyramid has 8 lines

But how do I make the top have 1 character then 3 then 5..?

function pyramid(character, rows, boolean) {
  let content = "\n";
  let contentAfter = character;
  let pyramid = new Array(rows);

for(let i=1; i<rows; i++) {
  contentAfter += character;
 let value = content + contentAfter;
 pyramid[i] = value;
}

return pyramid.join("")
}

Feel like I’ve tried everything!

you need to figure out the relationship there

Okay so I’ve managed to make the rows. But what about the spaces beforehand? Am I missing something obvious??

function pyramid(character, rows, boolean) {
  let content = "\n";
  let contentAfter = character;
  let pyramid = new Array(rows);

for(let i=1; i<rows; i++) {
  contentAfter += character.repeat(2);
 let value = content + contentAfter;
 pyramid[i] = value;
}


return character + pyramid.join("")
}

you need a similar formula for the spaces

Okay so I’ve gotten further but for some reason the commas aren’t removing eventhough I’ve used the .join() function with “”.

function pyramid(character, rows, boolean) {
  let content = "\n";
  let contentAfter = character;
  let pyramid = new Array(rows);
  let firstRow = (rows - 1) + character
   let contentBefore = rows - 1;
  let spacesArr = new Array(contentBefore).fill(" ");

  if(boolean===false) {
for(let i=1; i<rows; i++) {
  spacesArr.shift();
  spacesArr.join("");
  contentAfter += character.repeat(2);
 let value = content + spacesArr + contentAfter;
 pyramid[i] = value;
}}else {
  
}


return firstRow + pyramid.join("");
}

join returns a new value, so you need to capture that value in some way, like you have it now the return value is lost

hmm is there any other hints i could get? I’m not sure if I need to approach it differently. I’ve also insterted the true/false aspect but now it’s not looping :upside_down_face:

function pyramid(character, rows, boolean) {
  let content = "\n";
  let contentAfter = character;
  let pyramid = new Array(rows);
  let firstRow = (rows - 1) + character
  let contentBefore = rows - 1;
  let spacesArr = new Array(contentBefore).fill(" ");

  if(boolean==false) {
for(let i=1; i<rows; i++) {
  spacesArr.shift();
  spacesArr.join("");
  contentAfter += character.repeat(2);
 let value = content + spacesArr + contentAfter;
 pyramid[i] = value;
 return firstRow + pyramid.join("");
}

} if(boolean===true) {
  for(let i=1; i<rows; i++) {
  spacesArr.shift();
  spacesArr.join("");
  contentAfter += character.repeat(2);
 let value = content + spacesArr + contentAfter;
 pyramid[i] = value;
 return pyramid.join("") + "\n" + firstRow;
}
}}

PS Thankyou so much for the replies!!! They’re super helpful and super rapid. Very grateful for anything as this is super frustrating!

this line is still not doing anything so you can remove it

what happens when the return executes?

I took the return call out of the loop and it’s working now! but the comma problem is still there. Should I be using a different approach instead of .fill??

you need to fix the floating join

when you do this with arrays like spaceArr they are forced into strings, and you get the commas

this on a line on its own does nothing, it’s like it’s not there

Right so I’ve gotten the result… but the tests aren’t passing.

function pyramid(character, rows, boolean) {
  let content = "\n";
  let contentAfter = character;
  let pyramid = new Array(rows);
  let firstRow = (rows - 1) + character;
  let contentBefore = rows - 1;
  let spacesArr = new Array(contentBefore).fill(" ");
  let firstRowArr = new Array(contentBefore).fill(" ");

  if(boolean==false) {
for(let i=1; i<rows; i++) {
  spacesArr.shift();
  contentAfter += character.repeat(2);
 let value = content + spacesArr.join("") + contentAfter;
 pyramid[i] = value;
}
 return (firstRowArr.join("")+character) + pyramid.join("");

} if(boolean===true) {
  for(let i=(rows-1); i>0; i--) {
  spacesArr.shift();
  contentAfter += character.repeat(2);
 let value = content + spacesArr.join("") + contentAfter;
 pyramid[i] = value;
}
 return pyramid.join("") + "\n" + (firstRowArr.join("")+character);
}}

Why :smiling_face_with_tear:

Wait i’ve done it!!! was missing the “\n” Thankyou for the moral support!!

1 Like

good job! solving these labs on your own is great progress