-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (52 loc) · 1.94 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var buttons = document.querySelectorAll("#rock, #paper, #scissor");
var pResult, cResult;
function playerSelect() {
for (let index = 0; index < buttons.length; index++) {
buttons[index].addEventListener("click", function () {
document.getElementById("player").innerHTML = this.textContent;
pResult = this.id;
compSelect();
setTimeout(checkRes, 3500); // Delay checkRes to ensure it runs after compSelect has finished
});
}
}
function compSelect() {
var intervalId = null;
function startLoop() {
var index = 0;
intervalId = setInterval(function () {
document.getElementById("computer").innerHTML =
buttons[index].textContent;
index = (index + 1) % buttons.length;
}, 300);
}
function stopLoop() {
setTimeout(function () {
clearInterval(intervalId);
var randomIndex = Math.floor(Math.random() * buttons.length);
document.getElementById("computer").innerHTML =
buttons[randomIndex].textContent;
cResult = buttons[randomIndex].id;
}, 3000);
}
startLoop();
stopLoop();
}
function checkRes() {
if (pResult === "rock" && cResult === "scissor") {
document.getElementById("result").innerHTML = "You Win :)";
} else if (pResult === "rock" && cResult === "paper") {
document.getElementById("result").innerHTML = "Computer Wins :(";
} else if (pResult === "paper" && cResult === "rock") {
document.getElementById("result").innerHTML = "You Win :)";
} else if (pResult === "paper" && cResult === "scissor") {
document.getElementById("result").innerHTML = "Computer Wins :(";
} else if (pResult === "scissor" && cResult === "paper") {
document.getElementById("result").innerHTML = "You Win :)";
} else if (pResult === "scissor" && cResult === "rock") {
document.getElementById("result").innerHTML = "Computer Wins :(";
} else if (pResult === cResult) {
document.getElementById("result").innerHTML = "Draw :/";
}
}
playerSelect();