-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
93 lines (81 loc) · 3.32 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const jankenOptions = ['ROCK', 'PAPER', 'SCISSORS']
const buttons = document.querySelectorAll('.box-janken-player button')
buttons.forEach((button) => {
button.addEventListener('click', () => {
janken(playerChoice(button.textContent.trim()), computerChoice())
})
})
const computerChoice = () => {
const number = Math.floor(Math.random() * 3)
return jankenOptions[number]
}
const playerChoice = (choice) => {
return jankenOptions.find((item) => item === choice)
}
const changeImage = (won) => {
if (won.toUpperCase() === 'PLAYER') {
const jankenChangeImg = document.querySelector('.box-janken-winner img').src = 'jankenFotos/win.jpg'
} else if (won.toUpperCase() === 'CPU') {
const jankenChangeImg = document.querySelector('.box-janken-winner img').src = 'jankenFotos/loss.jpg'
} else {
const jankenChangeImg = document.querySelector('.box-janken-winner img').src = 'jankenFotos/draw.png'
}
}
const gameOverColors = (won) => {
if (won.toUpperCase() === 'PLAYER') {
const boxJankenComputer = document.querySelector('.box-janken-computer h2')
boxJankenComputer.style.color = 'red'
const boxJankenWinner = document.querySelector('.box-janken-winner h2')
boxJankenWinner.style.color = 'green'
} else if (won.toUpperCase() === 'CPU') {
const boxJankenComputer = document.querySelector('.box-janken-computer h2')
boxJankenComputer.style.color = 'green'
const boxJankenWinner = document.querySelector('.box-janken-winner h2')
boxJankenWinner.style.color = 'red'
} else {
const boxJankenComputer = document.querySelector('.box-janken-computer h2')
boxJankenComputer.style.color = 'white'
const boxJankenWinner = document.querySelector('.box-janken-winner h2')
boxJankenWinner.style.color = 'white'
}
}
const janken = (playerChoice, computerChoice) => {
const boxJankenComputer = document.querySelector('.box-janken-computer h2')
boxJankenComputer.textContent = computerChoice
const boxJankenWinner = document.querySelector('.box-janken-winner h2')
if(playerChoice === 'ROCK' && computerChoice === 'SCISSORS') {
boxJankenWinner.textContent = 'YOU WON!'
gameOverColors('PLAYER')
changeImage('PLAYER')
}
else if(playerChoice === 'SCISSORS' && computerChoice === 'ROCK') {
boxJankenWinner.textContent = 'YOU LOST'
gameOverColors('CPU')
changeImage('CPU')
}
else if(playerChoice === 'SCISSORS' && computerChoice === 'PAPER'){
boxJankenWinner.textContent = 'YOU WON'
gameOverColors('PLAYER')
changeImage('PLAYER')
}
else if(playerChoice === 'PAPER' && computerChoice === 'SCISSORS'){
boxJankenWinner.textContent = 'YOU LOST'
gameOverColors('CPU')
changeImage('CPU')
}
else if(playerChoice === 'PAPER' && computerChoice === 'ROCK'){
boxJankenWinner.textContent = 'YOU WON'
gameOverColors('PLAYER')
changeImage('PLAYER')
}
else if(playerChoice === 'ROCK' && computerChoice === 'PAPER'){
boxJankenWinner.textContent = 'YOU LOST'
gameOverColors('CPU')
changeImage('CPU')
}
else {
boxJankenWinner.textContent = 'DRAW!'
gameOverColors('DRAW')
changeImage('DRAW')
}
}