-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (58 loc) · 2.19 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
import { getRandomThrow, checkResult, } from './get-random-throw.js';
// defining DOM elements
const playButton = document.getElementById('play-button');
const resetButton = document.getElementById('reset-button');
const winCount = document.getElementById('win-count');
const lossCount = document.getElementById('loss-count');
const drawCount = document.getElementById('draw-count');
const gameDisplay = document.getElementById('game-display');
const matchResults = document.getElementById('match-results');
const computerThrows = document.getElementById('computer-throw');
// defining variables for win/loss/draw counts
let wins = 0;
let losses = 0;
let draws = 0;
// press play button
playButton.addEventListener('click', () => {
//pulls RPS from user radio button input
const userPlays = document.querySelector('input:checked');
const userChoice = userPlays.value;
// calls the function for computer's choice
const computerPlays = getRandomThrow();
const outcome = checkResult(userChoice, computerPlays);
// display what the computer plays
if (computerPlays === 'rock') {
computerThrows.textContent = ('Computer chose rock.');
} if (computerPlays === 'paper') {
computerThrows.textContent = ('Computer chose paper.');
} if (computerPlays === 'scissors') {
computerThrows.textContent = ('Computer chose scissors.');
// update the wins count
} if (outcome === 'win') {
wins++;
winCount.textContent = wins;
matchResults.textContent = 'You win!';
// update the losses count
} else if (outcome === 'lose') {
losses++;
lossCount.textContent = losses;
matchResults.textContent = 'You lose!';
// update the draws count
} else {
draws++;
drawCount.textContent = draws;
matchResults.textContent = 'You both draw!';
}
//display the results of the match
gameDisplay.classList.remove('hidden');
});
// reset button
resetButton.addEventListener('click', () => {
wins = 0;
losses = 0;
draws = 0;
winCount.textContent = wins;
lossCount.textContent = losses;
drawCount.textContent = draws;
gameDisplay.classList.add('hidden');
});