I have a recursive loop which for this question I've simplified to the following:
const move = (gameGrid) => {
if (Math.random() > 0.2) return false;
let newGameGrid = [...gameGrid];
return newGameGrid;
}
const play = (initGrid) => {
for (let player1Move=0; player1Move<9; player1Move++) {
grid1 = move(initGrid);
if (!grid1) continue;
for (let player2Move=0; player2Move<9; player2Move++) {
// Why is grid1 sometimes false here???
grid2 = move(grid1);
if (!grid2) continue;
play(grid2);
}
}
}
play([])
What you'll notice if you run this is that sometimes in the second play() loop the call to move(grid1) fails at [...gameGrid] because grid1 is false. However, this should be impossible because before the second loop executes grid1 is checked for a false value.
Any ideas what I'm missing here? I'm guessing grid1 is being overwritten but I'm not sure how or where.
if (!grid1) continue;basically state:if grid1 is false, continue to the for loop?break;also stops the current iteration but withcontinueyou do still stay in the loop.continueon MDN