AName
1
Tell us what’s happening:
I cant seem to figure out why my code isn’t passing the test
Your code so far
function pyramid (pattern,int,boolean){
if (boolean === false){
let pyramidS = "";
let arrNum = [1,3,5,7,9];
for (let i = 0;i < int; i++){
pyramidS += " ".repeat(int-i-1);
pyramidS += pattern.repeat(arrNum[i]);
pyramidS += "\n";
}
return pyramidS
}
else if (boolean === true){
let pyramidS = "";
let arrNum = [9,7,5,3,1];
for (let i = 0;i < int; i++){
pyramidS += " ".repeat(i);
pyramidS += pattern.repeat(arrNum[i]);
pyramidS += "\n";
}
return pyramidS
}
}
console.log(pyramid ("o", 4, false))
console.log(pyramid ("p", 5, true))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0
Challenge Information:
Build a Pyramid Generator - Build a Pyramid Generator
There should be a new line \n at the beginning of the returned string.
Currently, pyramid("p", 5, true) returns:
ppppppppp\n ppppppp\n ppppp\n ppp\n p\n
but it should return:
\nppppppppp\n ppppppp\n ppppp\n ppp\n p\n
AName
3
Here’s my new code. It still doesn’t pass the test.
function pyramid (pattern,int,boolean){
if(boolean === false){
let pyram = “”;
let count = 1;
for (let i = 0; i < int; i++) {
pyram += " ".repeat(int - i)
pyram += pattern.repeat(count);
pyram += “\n”
count += 2
}
return pyram
}
if(boolean === true){
let pyram = “”;
let count = int * 2 - 1;
for (let i = 1; i <= int; i++) {
pyram += " ".repeat(i);
pyram += pattern.repeat(count);
pyram += “\n”;
count -= 2;
}
return pyram
}
}
Assume int is 5 and boolean is false,
For the first row, the code creates 5 - 1 = 4 spaces, but it should be 3 spaces.
This causes every row to have one extra leading space.
Try this to compare the desired output with what your function currently returns:
console.log("\n o\n ooo\n ooooo\nooooooo\n")
console.log(pyramid("o", 4, false))
Assume int is 5 and boolean is true,
In this case, the first row will have 1 space, but it should have 0 spaces.
Again, this results in an extra leading space on every row.
See the difference:
console.log("\nppppppppp\n ppppppp\n ppppp\n ppp\n p\n")
console.log(pyramid("p", 5, true))
One last thing: the pyram variable should start with a new line \n, but you initialized it as an empty string.
What does it return?
Here are some troubleshooting steps you can follow. Focus on one test at a time:
- Are there any errors or messages in the console?
- What is the requirement of the first failing test?
- Check the related User Story and ensure it’s followed precisely.
- What line of code implements this?
- What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)
If this does not help you solve the problem, please reply with answers to these questions.
AName
6
I figured it out days ago thanks.. I just had to move one of the ‘newlines’ out of the loop.