Ensure the output starts with a newline.
You aren’t added the result of repeating to the variable in both loops.
Ensure the output starts with a newline.
You aren’t added the result of repeating to the variable in both loops.
Now I am getting the desired result but test not passing.
show your updated code
Here’s the code →
function pyramid(character, rows, isInverted) {
let level = "";
if(isInverted === false) {
for(let i = 1; i <= rows; i++) {
level += " ".repeat(rows-i) + character.repeat(2 * i -1) + "\n";
}
}
else {
for(let i = rows; i >= 1; i--) {
level += " ".repeat(rows-i) + character.repeat(2 * i -1) + "\n";
}
} return level;
}
console.log(pyramid("o", 4, false));
console.log(pyramid("p", 5, true));
Hello Ilenia, happy to see you again
I shared updated code above
Right now it is not easy to compare the expected result with the output, so we can use the JSON.stringify function
console.log(JSON.stringify(pyramid("o", 4, false)));
console.log(JSON.stringify(pyramid("p", 5, true)));
This outputs
" o\n ooo\n ooooo\nooooooo\n"
"ppppppppp\n ppppppp\n ppppp\n ppp\n p\n"
and you can compare it with the two desired outputs:
"\n o\n ooo\n ooooo\nooooooo\n"
"\nppppppppp\n ppppppp\n ppppp\n ppp\n p\n"
you are almost there, you are missing a thing at the beginning of the strings
Yes there should be one extra \n in beginning of both outputs
I don’t understand how to fix that single \n at beginning, due to loop it will get repeated and I only want it once. But why do I even need it ?
I fixed it, lesson passed but why do I even need it ? Please explain.
return "\n" + level;
it’s part of the requirements so it must be there
okay got it, thank you.
Thanks a lot @hasanzaib1389 @ILM and @Teller ![]()