Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

I am failing test cases 3 and 4 but I am not sure why.

My output string matches the expected return string. I’m not sure what is the problem. Am I overlooking something?

Your code so far

function pyramid(str, num, bool){
  let patt = [];
  // turn array into string
  let result = "";
  let count = 1;
    
  for(let i = 0; i < num; i++){
    // push char pattern to array
    patt.push(" ".repeat(num - (i + 1)) + str.repeat(count));
    // increase the number of char for next row
    count += 2;
    //console.log(patt);
  }
  // invert the pyramid
  if (bool){
    patt = patt.reverse();
  }

  // turn array into string
  for(let row of patt){
    result = result + "\n" + row;
  }

  // return the results
  result = result + "\n";
  let final = JSON.stringify(result);
  console.log(final);
  return final;

};

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

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Welcome to the forum @bjonet!

The way to test this is to wrap you function call in console.log(), not log at the end of the function. The function should only return the string.

Where in the instructions were you asked to apply JSON.stringify() to the result string?

When those issue are fixed, you can test your final string like this:

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

Happy coding!

1 Like

Thank you so much @dhess !! I now understand what I did wrong. I really appreciate your help.