Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

output seems accurate but still showing steps 3 and 4 failed

Your code so far

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

else {

 for(let j=num; j>=1; j--)
  {
    let space=" ".repeat(num-j);
    let pattern= str.repeat(2*j-1);
  result=space+pattern+space;
  console.log("\n"+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

you are relying on console.log instead of returning the requested output

add this at the bottom of your code to see what your function is returning and confront it with what it should return:

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"))

if i do this then it wud print only first line

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

else {

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

what happens to the value of result here? you are overwriting it each time instead of building it up

tried this instead but no luck

 for(let j=num; j>=1; j--)
  {
    //let space=" ".repeat(num-j);
    //let pattern= str.repeat(2*j-1);
  //result=space+pattern+space;
  result=" ".repeat(num-j)+str.repeat(2*j-1)+" ".repeat(num-j);
  //console.log("\n"+result);
  }

what are the differences between your output and the expected output?

1 Like

thanks it got fixed now :slight_smile: