function computerLogic(array) {
function random() {
var rand = Math.floor(Math.random() * 9);
if ((array[rand] === playerChoice) || (array[rand] === computerChoice)) {
random();
} else if (array[rand] === 0) {
array[rand] = computerChoice;
grid.item(rand).innerHTML = computerChoice;
} else {
alert("END OF THE GAME!");
console.log(array[rand]);
}
}
random();
}
so its part of my Tic Tac Toe code to put O or X to the grid. This function is being called in function handling clicks of users.Every time user clicks something then this function is called and updated array is send to it.
Im recursively calling function random() to random again number if it is in range of if statement.
I dont want to call it recursively but I have no idea how to make var rand to random again instead of calling whole random() function again.
just ignore last else statement.
here you can look at all code if its chaotic for you. http://ift.tt/1pAUd5s
Aucun commentaire:
Enregistrer un commentaire