-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
101 lines (67 loc) · 2.33 KB
/
app.js
File metadata and controls
101 lines (67 loc) · 2.33 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const choices = ['paper', 'rock', 'scissors'];
const buttons = document.querySelectorAll('.pick');
const scoreElement = document.getElementById('score_points');
const main = document.getElementById('main');
const selection = document.getElementById('selection');
const playAgain = document.getElementById('playAgain');
const user = document.getElementById('user');
const computer = document.getElementById('computer');
const win = document.getElementById('win');
const showRules = document.getElementById('showRules');
const openBtn = document.getElementById('open');
const closeBtn = document.getElementById('close');
let score = 0;
let userChoice = undefined;
buttons.forEach(button => {
button.addEventListener('click', () => {
userChoice = button.getAttribute('data-choice');
Winner();
});
});
playAgain.addEventListener('click', () => {
main.style.display = 'flex';
selection.style.display = 'none';
});
openBtn.addEventListener('click', () => {
showRules.style.display = 'flex';
});
closeBtn.addEventListener('click', () => {
showRules.style.display = 'none';
});
function Winner() {
const computerChoice = pickRandom();
updateSelection(user, userChoice);
updateSelection(computer, computerChoice);
if (userChoice === computerChoice) {
win.innerText = 'draw';
} else if (
(userChoice === 'paper' && computerChoice === 'rock') ||
(userChoice === 'rock' && computerChoice === 'scissors') ||
(userChoice === 'scissors' && computerChoice === 'paper')
) {
updateScore(1);
win.innerText = 'win';
} else {
updateScore(-1);
win.innerText = 'lose';
}
main.style.display = 'none';
selection.style.display = 'flex';
}
function updateScore(value) {
score += value;
scoreElement.innerText = score;
}
function pickRandom() {
return choices[Math.floor(Math.random() * choices.length)]
;
}
function updateSelection(selectionElement, choice) {
selectionElement.classList.remove('btn-paper');
selectionElement.classList.remove('btn-rock');
selectionElement.classList.remove('btn-scissors');
const img = selectionElement.querySelector('img');
selectionElement.classList.add(`btn-${choice}`);
img.src = `./images/icon-${choice}.svg`;
img.alt = choice;
}