Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

My code works but don’t know how it’s not passing for the tests.

Your code so far

const pyramid = (letter, rows, isDown) => {
  let pyramidStr = ""
  const newLine = "\n";
  const whiteSpace = " ";

  for (let i = 0; i < rows; i++) {
    pyramidStr += whiteSpace.repeat(rows - i - 1) + letter.repeat(i * 2 + 1) + newLine;
  }
  if (!isDown) {
    console.log(pyramidStr);
    return;
  } else {
    const reversedPyramid = pyramidStr.trimEnd().split(newLine).reverse().join(newLine);
    console.log(reversedPyramid);
    return;
  }
};

pyramid("o", 4,  false);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

what is your function returning?

I see that the 3rd and 4th criteria say that the code should return strings so now my code returns the final string and I made sure to the string starts with newline too but doesn’t pass still

  for (let i = 0; i < rows; i++) {
    pyramidStr += newLine + whiteSpace.repeat(rows - i - 1) + letter.repeat(i * 2 + 1);
  }
  if (!isDown) {
    console.log(pyramidStr);
    return pyramidStr;
  } else {
    const reversedPyramid = pyramidStr.trimEnd().split(newLine).reverse().join(newLine);
    console.log(reversedPyramid);
    return reversedPyramid;
  }
};

can you share the full function? I don’t know which parts you replaced

const pyramid = (letter, rows, isDown) => {
  let pyramidStr = ""
  const newLine = "\n";
  const whiteSpace = " ";

  for (let i = 0; i < rows; i++) {
    pyramidStr += newLine + whiteSpace.repeat(rows - i - 1) + letter.repeat(i * 2 + 1);
  }
  if (!isDown) {
    console.log(pyramidStr);
    return pyramidStr;
  } else {
    const reversedPyramid = pyramidStr.trimEnd().split(newLine).reverse().join(newLine);
    console.log(reversedPyramid);
    return reversedPyramid;
  }
};

pyramid("o", 4,  false);

Spaces are difficult to see in the extended version, so a trick to have your function output comparable with what the tests say is to use JSON.stringify

For example JSON.stringify(pyramid("o", 4, false)) gives "\n o\n ooo\n ooooo\nooooooo" and we can compare that with the expected value of "\n o\n ooo\n ooooo\nooooooo\n"

let’s put them one above the other, the first one is yours the second is the expected:

"\n   o\n  ooo\n ooooo\nooooooo"
"\n   o\n  ooo\n ooooo\nooooooo\n"

seeing the difference is the frist step to fix it, do you see the difference?

1 Like

I had seen the difference and have completed the task. Thank you very much!