-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (58 loc) · 2.03 KB
/
app.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Grabbing the relevant HTML elements
let userScoreDisplay = document.getElementById('youScore');
let cpuScoreDisplay = document.getElementById('cpuScore');
let resultText = document.getElementById('resultText');
let userIcon = document.getElementById('userIcon');
let cpuIcon = document.getElementById('cpuIcon');
const buttons = document.querySelectorAll('button')
//Variables
let userScore = 0;
let cpuScore = 0;
// Icon Options
const iconOptions = ["far fa-hand-rock", "far fa-hand-paper", "far fa-hand-scissors"]
buttons.forEach((button) => {
button.addEventListener('click', (e) => {
let randNum = () => Math.floor(Math.random() * 3);
//compare();
i = 0;
r = 0;
function loop() {
if (i < 3) {
setTimeout(() => {
console.log(i);
cpuIcon.className = iconOptions[i]
userIcon.className = iconOptions[i]
i++;
loop();
}, 70);
} else if (r < 6) {
i = 0
r++;
loop();
} else {
userIcon.className = button.className
cpuIcon.className = iconOptions[randNum()]
compare();
}
}
loop();
})
})
function compare() {
if ((userIcon.className == iconOptions[0] && cpuIcon.className == iconOptions[2]) ||
(userIcon.className == iconOptions[1] && cpuIcon.className == iconOptions[0]) ||
(userIcon.className == iconOptions[2] && cpuIcon.className == iconOptions[1])) {
userScore++;
userScoreDisplay.textContent = userScore;
resultText.textContent = 'YOU WIN'
console.log('You Win')
} else if (userIcon.className == cpuIcon.className) {
console.log('its a tie')
resultText.textContent = 'ITS A TIE'
} else {
console.log('you lose')
cpuScore++;
resultText.textContent = 'YOU LOSE'
cpuScoreDisplay.textContent = cpuScore;
}
}