-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
83 lines (60 loc) · 2.19 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
71
72
73
74
75
76
77
78
79
80
81
82
83
function getComputerChoice() {
let choices= ['Rock' , 'Paper' , 'Scissors' ]
let ans = choices[Math.floor(Math.random() * 3)]
return ans
}
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 0: result.innerText='Its a Draw'
break;
case 1: result.innerText='You win!'
break;
case -1: result.innerText='You Lose!'
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() {
// use querySelector to select all RPS Buttons
let rpsButtons= document.querySelectorAll('.rpsButton')
rpsButtons.forEach(rpsButton => {
rpsButton.onclick = () => onClickRPS(rpsButton)
})
// Add a click listener to the end game button that runs the endGame() function on click
let endGameBtn= document.getElementById('endGameButton')
endGameBtn.onclick=()=> endGame()
}
// ** endGame function clears all the text on the DOM **
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()