-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (60 loc) · 2.01 KB
/
script.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
function getComputerChoice() {
let rpsChoices = ['Rock', 'Paper', 'Scissors']
let computerChoice = rpsChoices[Math.floor(Math.random() * 3)]
return computerChoice
}
function getResult(playerChoice, computerChoice) {
let score;
if (playerChoice === computerChoice) {
score = 0
} else if (playerChoice === 'Rock' && computerChoice === 'Scissors') {
score = 1
} else if (playerChoice === "Paper" && computerChoice === "Rock") {
score = 1
} else if (playerChoice === "Scissors" && computerChoice === "Paper") {
score = 1
} else {
score = -1
}
return score
}
function showResult(score, playerChoice, computerChoice) {
let result = document.getElementById('result')
switch (score) {
case -1:
result.innerText = `You Lose!`
break;
case 0:
result.innerText = `It's a Draw!`
break;
case 1:
result.innerText = `You Win!`
break;
}
let playerScore = document.getElementById('player-score')
let hands = document.getElementById('hands')
playerScore.innerText = `${Number(playerScore.innerText) + score}`
hands.innerText = `👱 ${playerChoice} vs 🤖 ${computerChoice}`
}
function onClickRPS(playerChoice) {
const computerChoice = getComputerChoice()
const score = getResult(playerChoice.value, computerChoice)
showResult(score, playerChoice.value, computerChoice)
}
function playGame(){
let rpsButtons = document.querySelectorAll('.rpsButton')
rpsButtons.forEach(rpsButton => {
rpsButton.onclick = () => onClickRPS(rpsButton)
})
let endGameButton = document.getElementById('endGameButton')
endGameButton.onclick = () => endGame()
}
function endGame() {
let playerScore = document.getElementById('player-score')
let hands = document.getElementById('hands')
let result = document.getElementById('result')
playerScore.innerText = ''
hands.innerText = ''
result.innerText = ''
}
playGame()