I cant wrap my head around this someone kindly assist
Your code so far
function pyramid(pattern,rows,isUpSideDown){
let result="/n";
let maxWidth=2*rows-1;
if(isUpSideDown){
for(let i=rows;i>=1;i--){
let numPatterns=2*i-1;
let numSpaces= (maxWidth-numPatterns)/2;
result += "".repeat(numSpaces)+pattern.repeat(numPatterns)+"".repeat(numSpaces)+ "/n";
}
}else{for(let i=1;i<=rows;i++){
let numPatterns=2*i-1;
let numSpaces= (maxWidth-numPatterns)/2;
result += "".repeat(numSpaces)+pattern.repeat(numPatterns)+"".repeat(numSpaces)+ "/n";
}
}
return result
}
pyramid("o", 4, false)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Challenge Information:
Build a Pyramid Generator - Build a Pyramid Generator
when it should display: "\n o\n ooo\n ooooo\nooooooo\n"
You need to look at your spaces as you don’t have any. You can see from the tests that you don’t need spaces after the pattern character on each line so you can remove that in your code.
To add spaces you need to put a space between your quotes. You are currently repeating empty strings.
I suggest you log this:
console.log(JSON.stringify(pyramid(“o”, 4, false))); and compare your result to the test results.
To pass, the newlines, spaces and characters have to match the test results. You don’t need to repeat the spaces after the pattern character so remove that.
There are a few issues with your code:
The value of your numSpaces and maxWidth variables - what are you trying to with this formula? This needs some work.
(maxWidth-numPatterns)/2;
The condition in your first if statement isn’t right. The instructions say if the third argument is false the vertex faces upwards. Your condition does the same thing regardless of how the third argument is set.
The syntax of your newline character in your result variable is wrong. It should be \n.
Your newline character is not being repeated. I suggest you remove it from the result variable to before your space repeat code.
Remember it has to start and end with a newline character. If you look at the test codes you will see there is an odd number of newlines. One for each line of code and a final one. You will have to add that final one somewhere else.