Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

My code is returning the expected result but the cant complete it

Your code so far

function pyramid(char, rows, inverted) {
let result = “”;
if (!inverted) {
for(let i = 1; i <= rows; i++){
result += “\n” + " ".repeat(rows - i) + char.repeat(i * 2 - 1);
}
return result;
} else {
for(let i = rows; i >= 1; i–){
result += “\n” + " ".repeat(rows - i) + char.repeat(i * 2 - 1);
}
return result;
}
}

console.log(pyramid(“o”, 4, false));
console.log(pyramid(“p”, 5, true));

function pyramid(char, rows, inverted) {
  let result = "";
  if (!inverted) {
    for(let i = 1; i <= rows; i++){
      result += "\n" + " ".repeat(rows - i) + char.repeat(i * 2 - 1);
    }
    return result;
  } else {
    for(let i = rows; i >= 1; i--){
      result += "\n" + " ".repeat(rows - i) + char.repeat(i * 2 - 1);
    }
    return result;
  }
}

console.log(pyramid("o", 4, false));
console.log(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/137.0.0.0 Safari/537.36

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

1 Like

Try adding this code to the end of your script file so you can compare what is expected to what your code is actually producing:

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

that actually helped me solve the problem. It was missing “\n” at the end so I reajusted the code and it worked. Many thanks

2 Likes

Thanks for that nifty test! Squashed my bug over here too.