-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps.js
59 lines (49 loc) · 1.92 KB
/
rps.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
let userInput = document.getElementById('userChoice')
let computerGenerated = document.getElementById('computerChoice')
let resultElement = document.getElementById('result')
let resetButton = document.getElementById('resetButton')
const choices = ["Rock" , "Paper" , "Scissors"]
function getComputerChoice(){
return choices[Math.floor(Math.random() * choices.length)]
}
function determineWinner(userChoice , computerChoice){
if (userChoice === computerChoice){
return "It's a tie"
}
if ((userChoice == "Rock" && computerChoice == "Scissors") ||
(userChoice == "Scissors" && computerChoice == "Paper") ||
(userChoice == "Paper" && computerChoice == "Rock")){
return "You Win"
}
else {
return "Computer Wins"
}
}
function handleChoice(userChoice){
let computerGuess = getComputerChoice()
userInput.textContent = userChoice
computerGenerated.textContent = computerGuess
resultElement.textContent = determineWinner(userChoice , computerGuess)
disableButtons()
resetButton.style.display = "inline-block";
}
function disableButtons(){
const buttons = document.querySelectorAll('.choice')
buttons.forEach(button=>{
button.disabled = true
})
}
function resetGame(){
userInput.textContent = "None"
computerGenerated.textContent = "None"
resultElement.textContent = ''
resetButton.style.display = "none"
const buttons = document.querySelectorAll('.choice')
buttons.forEach(button => {
button.disabled = false
})
}
document.getElementById('rock').addEventListener('click' , () => handleChoice('Rock'))
document.getElementById('paper').addEventListener('click' , () => handleChoice('Paper') )
document.getElementById('scissors').addEventListener('click' , () => handleChoice('Scissors'))
resetButton.addEventListener('click' ,resetGame)