Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

Not sure if this is the optimal way to run the code but it should be returning the string that is needed to pass, not sure what the issue is. I do see that for step 4 the string to pass has a “\p” for the second new line command instead of a “\n”, not sure what that is about but that doesn’t explain why step 3 is failing as well. Please help!

Your code so far

const pyramid = (str, rows, boolean) => {
  const generator = str;
  let times = generator;
  let multiply = rows;
  let space = ' ';
  const array = [];
  if (boolean === false) {
    for (let i = 0; i < rows; i++){
       array.push('\n');
       array.push(space.repeat(multiply - 1));
       array.push(times);
       times += generator + generator;
       multiply--;  
    }
    return array.join('').toString();
   } else if (boolean === true) {
     for (let i = 0; i < rows; i++){
       array.unshift(space.repeat(multiply));
       array.unshift('\n');
       array.unshift(times);
       times += generator + generator;
       multiply--; 
       
     }
   }
   return array.join('').toString();
  }
  console.log(pyramid('o', 4, false));
  console.log('\n   o\n  ooo\n ooooo\nooooooo\n')
  console.log(pyramid('p', 5, true));
  console.log('\nppppppppp\p ppppppp\n  ppppp\n   ppp\n    p\n');

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

The issue is in the text of the test, I will open an issue to fix this, the test is looking for the string "\nppppppppp\n ppppppp\n ppppp\n ppp\n p\n"

let’s look at your code instead:

we can see the string in the same format as what is shown in the tests by using JSON.stringify (it has not been introduced yet, I think, but it’s useful in these situations)

Using that we can see that JSON.stringify(pyramid('o', 4, false)) gives back "\n o\n ooo\n ooooo\nooooooo"

the expected string is "\n o\n ooo\n ooooo\nooooooo\n"
so let’s confront the two:

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

do you see the difference?