Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

I don’t understand why my code isn’t passing if someone could help, the output seems to be correct.

Your code so far

const row = []
let result = ""
const pyramid = (char, int, inverted) => {
  let i = 1;
  
while(i <= int){
  result = "\n"

    let rowTemplate = `${" ".repeat(int - i)}${char.repeat(2 * i - 1)}${" ".repeat(int - i)}`
    if(inverted === false){
      i++
      row.push(rowTemplate)
    }else{
      i++
      row.unshift(rowTemplate)
    }
    }
   for (let line of row) {
    result =`${result}${line}\n`
  }
    return result
 }
  
   
  
  

console.log(pyramid("o", 4, false));






//  for(let i = 1; i <= int; i++){
    
//       let rowTemplate = `${" ".repeat(int - i)}${char.repeat(2 * i - 1)}${" ".repeat(int - i)}`
//       if(inverted === false){
//        row.push(`${rowTemplate}`)
//      }else{
//         row.unshift(rowTemplate)  
//     }
     
//     }
//     let result = ""
//     for(let line of row){
//       result = `${result}${line}\n`
//     }
  
//     return `${result}`
//   }

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Hi @lynders and welcome to our community!

Your return string has additional trailing whitespaces, compared with the expected output.

thanks! Could you tell me which part you are talking about so I understand further?

you can do this console.log(JSON.stringify(pyramid("o", 4, false))); and you can compare your pyramid with the expected string

the trailing spaces are on each line

1 Like

Ok I see the spaces now thank you, but I don’t know how to fix them… could you maybe explain why these extra spaces are happening, or point me to the piece of code that’s the problem

why don’t you check your code? they are coming from somewhere

which is the code that creates each line?

I’ve tried a bunch of different variations of things and its still not passing, the code that creates a new line would be the rowTemplate yeah? I’ve gotten rid of the space at the end, and it looks right even with JSONstringify but it still won’t pass.

can you share your updated code?

const row = []

let result = ""

const pyramid = (char, int, inverted) => {
for(let i = 1; i <= int; i++){
    result = "\n"
      let rowTemplate = `${" ".repeat(int - i)}${char.repeat(2 * i - 1)}${"".repeat(int - i)}`
      if(inverted === false){
       row.push(`${rowTemplate}`)
     }else{
        row.unshift(`${rowTemplate}`)  
    }
    
     
    }
    for(let line of row){
    result =`${result}${line}\n`
    }
    return result
    
    
  }

which tests are failing now?

same as before 3 and 4

    1. pyramid("o", 4, false) should return "\n o\n ooo\n ooooo\nooooooo\n".
    1. pyramid("p", 5, true) should return "\nppppppppp\n ppppppp\n ppppp\n ppp\n p\n".

Your solution works from my end. Please try one of the following steps to move forward.

Click on the “Restart Step” button and force a refresh of your page with CTRL + F5 then try to paste the code in again.

or - Try the step in incognito or private mode.

or - Disable any/all extensions that interface with the freeCodeCamp website (such as Dark Mode, Ad Blockers, or Spellcheckers), and set your browser zoom level to 100%. Both of these factors can cause tests to fail erroneously.

or - Ensure your browser is up-to-date or try a different browser.

I hope one of these will work for you.

Thank you for checking. I tried it on a different browser, disabled all extensions, zoom level, tried it in incognito updated browser and computer, and restarted the lesson multiple times, and it’s still not passing, I don’t know what else it could be. Not sure what else to do but move on at this point. But I appreciate all your help.

I tried your exact code and it passes

are you sure you are using this exact code?

It wasn’t earlier but I just copied pasted it and it worked lol not sure why it did this time, because I kept the code I was trying earlier and it looks the exact same. Oh well. lol Thanks for your help!! :smile: