Closed Clund1 closed 9 months ago
The function to randomize an array of 5 values and then push to the playing field isnt working as intended
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; }
No error message, but instead of getting 5 arrays of 5 values in the forest object, Im getting 5 arrays with 25 Values
I think its the push method adding to each existing array
MDN, looking up other methods that could possible create an object in a similar fashion
https://github.com/Clund1/Project-1---GAME/blob/main/script.js
Have you tried console.logging forest in the generateHidingSpots() function?
forest
generateHidingSpots()
Its like its doubling up the number of values with each function call
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)
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