I can't figure this out. What's happening is that when I click a div in playerMove()
it should make the var clickedId that id. It works if I click one div and then another div, but if I click the same div twice the variable doubles and adds one. For example:
1st click = 3 2nd click = 4 This works fine
1st click = 3
2nd click = 3
This creates something like var clickedId = 3 3 3 3
Here's the fiddle jsfiddle.net/61cyw2s8
$(document).ready(function() {
//Global Arrays
var player = [];
var comp = [];
//function for RandomInt 1-4
function randomInt() {
return Math.floor(Math.random() * (4 - 1 + 1) + 1);
}
//hover function
function hover(value) {
$("#" + value).addClass("hover");
setTimeout(function() {
$("#" + value).removeClass("hover");
}, 500);
};
//Comp Move
var hoverCount = 0
function addCompMove() {
comp.push(randomInt());
compHover();
}
function compHover() {
setTimeout(function() {
console.log(hoverCount);
hover(comp[hoverCount]);
hoverCount++;
if (hoverCount < comp.length) {
compHover();
}
}, 1000)
playerMove();
};
//Player Move
function playerMove() {
$(".playbox").on("click", function() {
var clickedId = parseInt(this.id);
console.log(clickedId);
player.push(clickedId);
hover(clickedId);
correctMove();
})
}
//Testing
function arrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (var i = arr1.length; i > -1; i--) {
if (arr1[i] !== arr2[i])
return false;
}
return true;
};
function correctMove() {
if (arrayEqual(comp, player)) {
hoverCount = 0;
player.length = 0;
addCompMove();
} else {
playerMove();
}
};
//Start
$("#start").bind("click", function() {
addCompMove();
});
});