Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

My code doesnt pass number 3 and 4 and I cant figure out the bug. kindly guide me:

// running tests
3. pyramid(“o”, 4, false) should return “\n o\n ooo\n ooooo\nooooooo\n”.
4. pyramid(“p”, 5, true) should return “\nppppppppp\n ppppppp\n ppppp\n ppp\n p\n”.
// tests completed
// console output
ppppppppp
ppppppp
ppppp
ppp
p

0
000
00000
0000000

Your code so far

function pyramid(string, integer, boolean){
  const rows = [];
  let results = "";
  function padRow(rowNumber, rowCount) {
  return " ".repeat(rowCount - rowNumber) + string.repeat(2 * rowNumber - 1) + " ".repeat(rowCount - rowNumber);
}
  if (boolean === true) {
  for (let i = 1; i <= integer; i++ ) {
    rows.unshift(padRow(i, integer));
  }

  } else {
    for (let i = 1; i <= integer; i++ ) {
    rows.push(padRow(i, integer));
  }
  }
  for(let row of rows){
   results += row + "\n";
  }
   return results;
}
  console.log(pyramid("p", 5, true)); 
  console.log(pyramid("0", 4, false));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.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

Hi @Amunyelet-Ojala

Looks like your function is adding extra spaces for the levels.

Happy coding

1 Like

Thank you. I have managed to figure it. I will happily move to the next project!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.