-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
61 lines (56 loc) · 2.13 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
const buttons = document.querySelector('.buttons');
const playerImage = document.getElementById('player_img');
const computerImage = document.getElementById('computer_img');
const middleText = document.querySelector('.middle_text');
const computerCount = document.querySelector('.computer--count');
const playerCount = document.querySelector('.player--count');
const resetButton = document.getElementById('reset_button');
buttons.addEventListener('click', (e) => {
if (e.target.classList.contains('rock')){
playerImage.src = './img/rock_player.png'
computerPlay('rock')
}
if (e.target.classList.contains('paper')) {
playerImage.src = './img/paper_player.png'
computerPlay('paper')
}
if (e.target.classList.contains('scissors')) {
playerImage.src = './img/scissors_player.png'
computerPlay('scissors')
}
})
function computerPlay(player) {
let arr = ['rock', 'paper', 'scissors']
let computer = arr[Math.floor(Math.random() * 3)]
computerImage.src = `./img/${computer}_computer.png`
count(player, computer)
}
let playerWins = 0;
let computerWins = 0;
function count (player, computer) {
if (player === computer) {
middleText.innerHTML = `It's a tie`
} else if ((player === 'rock' && computer === 'scissors') || (player === 'paper' && computer === 'rock') || (player === 'scissors' && computer === 'paper')) {
playerWins++;
playerCount.innerHTML = `${playerWins}`
middleText.innerHTML = `You win, computer's choice was ${computer}`
} else {
computerWins++;
computerCount.innerHTML = `${computerWins}`
middleText.innerHTML = `You lose, computer's choice was ${computer}`
}
if (playerWins === 5 || computerWins === 5){
winner(playerWins)
}
}
function winner (playerWins){
if (playerWins === 5) {
middleText.innerHTML = `You won, congrats <br> resetting...`
} else {
middleText.innerHTML = `You lost, you'll get it next time <br> resetting...`
}
setTimeout(() => window.location.reload(), 2000);
}
resetButton.addEventListener("click" , () => {
window.location.reload();
})