Build a Pyramid Generator lab - Full-Stack Dev Course

Tell us what’s happening:

I cannot get the first line to get “\n” and past the code.
I did get it by putting another line before the for loop but the code still did not pass.
I have everything else. The pyramid is formed with vertex up or down based on the Boolean but I cannot get #3 and #4 to pass.

Please check my code and let me know where I am going wrong. I even did the pyramid generator workshop in the JavaScript algorithm course.

Your code so far


function pyramid(str, int, bol){ 
  const rows = [];
let results = "";
  if (bol === false){
  for (let i = 1; i <= int; i++){
rows.push(" ".repeat(int - i) + str.repeat((i*2 -1) ) + " ".repeat(int - i));
}} else {
   for (let i = 1; i <= int; i++){
rows.unshift(" ".repeat(int - i) + str.repeat((2 * i) -1 ) + " ".repeat(int - i)) + "\n";
} }
for (const row of rows){
  results += row + "\n";
  }
  return results;
}
let test = pyramid("o", 4, false);

console.log(test);

Your browser information:

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

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Let’s use this code to print your outputs in a way that is wasily comparable

console.log(JSON.stringify(pyramid("o", 4, false)));
console.log(JSON.stringify("\n   o\n  ooo\n ooooo\nooooooo\n"));
console.log(JSON.stringify(pyramid("p", 5, true)));
console.log(JSON.stringify("\nppppppppp\n ppppppp\n  ppppp\n   ppp\n    p\n"));
"   o   \n  ooo  \n ooooo \nooooooo\n"
"\n   o\n  ooo\n ooooo\nooooooo\n"
"ppppppppp\n ppppppp \n  ppppp  \n   ppp   \n    p    \n"
"\nppppppppp\n ppppppp\n  ppppp\n   ppp\n    p\n"

it doesn’t look like the newline character at the beginning is being added like you say

also you have space characters after each line that should not be there (this is not an exact trasposition of the pyramid generator workshop, you can’t use the same exact code)

1 Like

passed it using another if/else inside the for loop to handle the i=0 and add the \n. Also resolved the space issue with your help using the JSON.stringify() method.

Thank you!!

1 Like