sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

Function only returning 1 Array when i ask for five? #232

Closed Clund1 closed 9 months ago

Clund1 commented 9 months ago

What's the problem you're trying to solve?

The function to randomize an array of 5 values and then push to the playing field isnt working as intended

Post any code you think might be relevant (one fenced block per file)

function initialLaunch() {
    forest = [
        generateHidingSpots(),
        generateHidingSpots(),
        generateHidingSpots(),
        generateHidingSpots(),
        generateHidingSpots()
    ]
    console.log(forest)
    gameFieldEl.forEach((box) => {
        box.addEventListener('click', (e) => {
            playerTurn(e);
        });
    })
render()
}
function generateHidingSpots() {
    for (let i = 0; i < 5; i++) {
      const randomNumber = [Math.floor(Math.random() * 8)]; 
    forest.push(randomNumber);
    }
    i=0
    return forest;
}

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

No error message, but instead of getting 5 arrays of 5 values in the forest object, Im getting 5 arrays with 25 Values

What is your best guess as to the source of the problem?

I think its the push method adding to each existing array

What things have you already tried to solve the problem?

MDN, looking up other methods that could possible create an object in a similar fashion

Paste a link to your repository here

https://github.com/Clund1/Project-1---GAME/blob/main/script.js

nayaba commented 9 months ago

Have you tried console.logging forest in the generateHidingSpots() function?

Clund1 commented 9 months ago
Screenshot 2023-12-04 at 4 06 45 PM

Its like its doubling up the number of values with each function call