Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

i need some help with this i have the right ouput but it cant pass

Your code so far

function pyramid(str,num,bol){
  if (bol === true){
  for (let i=1;i <= num;i++){
let space=' '.repeat(num - i);
let mid=str.repeat(2 * i - 1);
let result =space + mid + space;
console.log(result)
  }
  }else {
    for (let i=num;i >= 1;i--){
let space=' '.repeat(num - i);
let mid=str.repeat(2 * i - 1);
let result =space + mid + space;
console.log(result)
    }
  }

}
console.log(pyramid('p', 4,true))

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

From User Story #5:

The pyramid function should return a string…

Also, you can add this to the bottom of your script file to see if your code is generating what is expected:

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

Tell us what’s happening:

is this exercise passable? cause i think am stuck.

Your code so far

function pyramid(str, num, bol) {

  let lines = [];

  let width = 2 * num - 1;
  if (bol === true) {

    for (let i = 1; i <= num; i++) {

      let row= 2 * i -1 

      let space = ' '.repeat(Math.floor((width - row) / 2));
      let mid = str.repeat(row);

      let result = space + mid + space;
      lines.push(result)
    }
  } else {

    for (let i = num; i >= 1; i--) {
      let row= 2 * i -1 

      let space = ' '.repeat(Math.floor((width - row) / 2));
      let mid = str.repeat(row);

      let result = space + mid + space;
      lines.push(result)
    }
  }
  return lines.join('\n');
}
console.log(pyramid('o', 4, true))

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

yes it is passable

you can confront your output with the required one by pasting this below your code

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

but i cant find the issue

do you mean you do not see the differences between thes two lines?

Actual:    "ooooooo\n ooooo \n  ooo  \n   o   "
Expected:  "\n   o\n  ooo\n ooooo\nooooooo\n"

and these two lines?

Actual:    "    p    \n   ppp   \n  ppppp  \n ppppppp \nppppppppp"
Expected:  "\nppppppppp\n ppppppp\n  ppppp\n   ppp\n    p\n"

ohh i’ve seen thanks

In future, please do not open multiple topics to ask about the same challenge or lab. Instead use the topic you have opened for it already to update us on your questions and code.

I merged your duplicates into one.

okay i understand and i solved it already

1 Like

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