Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

I am having issues with the output of my program meeting the test cases. I ‘m printing the results of my function to the console and the sample from the test-cases to the console to compare the differences. They both appear the exact same. Same number of characters, number of whitespaces, and the newlines before/after. I am confused as to why this isn’t passing the test cases.

Your code so far

function pyramid(string, numRows, boolVal) {
  let n;
  let x;
  let count = 0;
  let masterPattern = "";

  
  if (boolVal === true) {
    n = 2 * numRows - 1;
    x = 0;

    while (count < numRows) {
      masterPattern += "\n" + " ".repeat(x) + string.repeat(n) + " ".repeat(x);

      n = n - 2;
      x++;
      count++;
    }

  } else {
    n = 1;
    x = numRows - 1;

    while (count < numRows) {
      masterPattern += "\n" + " ".repeat(x) + string.repeat(n) + " ".repeat(x);

      n += 2;
      x--;
      count++;
    }

  }
  

  return masterPattern;
}

// Debugging
console.log(pyramid("p", 5, true));
console.log("\nppppppppp\n ppppppp\n  ppppp\n   ppp\n    p\n");

Test Cases:

3. pyramid("o", 4, false) should return "\n   o\n  ooo\n ooooo\nooooooo\n".

4. pyramid("p", 5, true) should return "\nppppppppp\n ppppppp\n  ppppp\n   ppp\n    p\n".

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

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

Try adding this code so you can compare actual to expected.

1 Like

This actually helped a lot and I was able to solve the Lab, thank you! So, just to sort of picture this…an array is considered a special type of object or rather just simply an object. So, you’re able to apply the JSON.stringify() method to it which works on JS objects.

This reference might help:

JSON.stringify() - JavaScript | MDN

1 Like