Tell us what’s happening:
I saw that others have taken a very different approach to this but my code does the job without issue. I don’t understand why it doesn’t pass the test.
I spaces before and after the pyramids are not pretty I admit but I don’t understand their purpose anyway.
Any help is appreciated. Thanks in advance.
Your code so far
function pyramid(char, rows, isFlipped) {
console.log(" ");
// Upright
if (isFlipped === false) {
let size = 1;
for (let i = 0; i < rows; i++) {
console.log(" ".repeat(rows - i) + char.repeat(size));
size+= 2;
}
console.log(" ");
}
// Upside down
else {
let size = rows* 2 - 1;
for (let i = 0; i < rows; i++) {
console.log(" ".repeat(i) + char.repeat(size));
size-= 2;
}
console.log(" ");
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Challenge Information:
Build a Pyramid Generator - Build a Pyramid Generator
https://www.freecodecamp.org/learn/full-stack-developer/lab-pyramid-generator/lab-pyramid-generator
Still don’t work :((
function pyramid(char, rows, isFlipped) {
// Upright
if (isFlipped === false) {
let size = 1;
let result = "";
for (let i = 0; i < rows; i++) {
result += " ".repeat(rows - i) + char.repeat(size) + "\n";
size+= 2;
}
return result;
}
// Upside down
else {
let size = rows* 2 - 1;
result = "";
for (let i = 0; i < rows; i++) {
result += " ".repeat(i) + char.repeat(size) + "\n";
size-= 2;
}
return result;
}
}
Teller
April 18, 2025, 3:55am
4
The pyramid needs to have a new line at the end.
Check for a new line at the start and end for both types of pyramids.
function pyramid(char, rows, isFlipped) {
// Upright
if (isFlipped === false) {
let size = 1;
let result = "";
for (let i = 0; i < rows; i++) {
result += " ".repeat(rows - i) + char.repeat(size) + "\n";
size+= 2;
}
return "\n" + result + "\n";
}
// Upside down
else {
let size = rows* 2 - 1;
result = "";
for (let i = 0; i < rows; i++) {
result += " ".repeat(i) + char.repeat(size) + "\n";
size-= 2;
}
return "\n" + result + "\n";
}
}
Teller
April 18, 2025, 4:21am
6
Add a console.log() for each of the pyramid() calls using the arguments from the tests.
For "o" highlight the console to see the spacing and line breaks.
For "p" see if you can figure out what is happening.
Now the “p” works but the “o” doesn’t. I am so confused and brain is starting to hurt.
By the way, since I have you here. How trash is my code? Is it good enough or should I reconsider the entire thing?
function pyramid(char, rows, isFlipped) {
// Upright
if (isFlipped === false) {
let size = 1;
let result = "";
for (let i = 0; i < rows; i++) {
result += " ".repeat(rows - i) + char.repeat(size) + "\n";
size+= 2;
}
return "\n" + result;
}
// Upside down
else {
let size = rows * 2 - 1;
let result = "";
for (let i = 0; i < rows; i++) {
result += " ".repeat(i) + char.repeat(size) + "\n";
size-= 2;
}
return "\n" + result;
}
}
console.log(pyramid("o", 4, false));
console.log(pyramid("p", 5, true));
Teller
April 18, 2025, 4:47am
8
Notice how the top pyramid is further from the edge of the console, compared to the second pyramid.
Your code is easy to read, good use of comments.
Thank you for the kind words. But if I am lacking somewhere, I’d appreciate the advice.
For example, I did a dirty solution and just added a minus 1 here to pass the test.
result += " ".repeat(rows - i - 1) + char.repeat(size) + "\n";
Not sure if there is a proper way to do it but like I said, my brain really dead at this point.
But yeah, thank you so much for the help.