Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

Please help, I got the results but still not passing.

Your code so far

function pyramid(str,num,bolean)
{
  let result = "\\n";
    
    if(bolean==false)
    {
      for(let i=1; i<=num; i++)
         {
    let space=" ".repeat(num-i);
    let pattern=str.repeat(2*i-1)+"\\n";
  result=result+space+pattern;
  
       }  console.log(result);
    }
    
 
else {

 for(let j=num; j>=1; j--)
  {
    let space=" ".repeat(num-j);
    let pattern= str.repeat(2*j-1)+"\\n";
  result=result+space+pattern;
  }   console.log(result);
 
}
  
return result;
}
pyramid("o", 4, false);
pyramid("p", 5, true);

 

Your browser information:

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

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

There is just one problem here. I see you wanted to add “newline characters”, but what the exercise ment by that is to really add a newline (and not display the newline character). You can do so or by using the newline character or by using templates literals.

Anything else is good ! Hope this helped, enjoy coding ! :slight_smile:

what is your function returning?

Little exercises like the pyramid generator are great for really understanding loops and how string building works in JavaScript. At first it feels simple, but once you start controlling spacing and alignment it actually forces you to think carefully about the logic. Debugging each step of the loop usually makes the pattern much clearer.

I have tried with /n only and get the pyramid shape results for both true and false requests, but still not passing.

got it now, thanks alot.:joy:

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