I’m working on a project and am writing an if statement inside of a for loop. Everything works fine with the shipPos for e.x when I try to console log them (console.log(shipPos[i]) but when I do an if statement and load the websites it just get stuck at loading forever.
if(element.pos.length === 0){
let shipPos = [];
let validPos;
do {
element.pos = [getRandomInt(9), getRandomInt(9)];
validPos = true;
for(let i = 0; i < element.length; i++){
const [x, y] = [element.pos[0] + i, element.pos[1]];
if(element.pos[0] + element.length - 2 >= 9 || element.pos[1] + element.length - 2 >= 9){
validPos = false;
break;
}
shipPos.push([x,y]);
}
for(let i = 0; i < shipPos.length; i++){
const [x, y] = [shipPos[i][0], shipPos[i][1]];
if(x === 4){ //example if statement
validPos = false;
}
}
} while (!validPos);
}
I’ve removed the if statement and it works perfectly so I dont really know what the problem is
1