-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (54 loc) · 1.84 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 computerChoiceDisplay = document.getElementById('computer-choice') //declaring the variable computerChoice by the id in the html doc file that is computer choice
const userChoiceDisplay = document.getElementById('user-choice')
const resultDisplay = document.getElementById('result')
//
const possibleChoices = document.querySelectorAll('button')
//For user choice
let userChoice
possibleChoices.forEach(possibleChoice => possibleChoice.addEventListener('click', (event) =>{
userChoice = event.target.id
userChoiceDisplay.innerHTML = userChoice
generateComputerChoice()
getResult()
}))
//For computer Choice
let computerChoice
function generateComputerChoice() {
const randomNum = Math.floor(Math.random() * 3) + 1 // or you can use possibleChoices.length which is 3 b/c 3 buttons
if( randomNum === 1) {
computerChoice = 'rock'
}
if(randomNum === 2) {
computerChoice = 'paper'
}
if(randomNum === 3) {
computerChoice = 'scissors'
}
computerChoiceDisplay.innerHTML = computerChoice
}
//results
let result
function getResult() {
if(computerChoice === userChoice) {
result = 'Its a Draw!'
}
if(computerChoice === 'rock' && userChoice === "scissors") {
result = 'you lost!'
}
if(computerChoice === 'rock' && userChoice === "paper") {
result = 'you won!'
}
if(computerChoice === 'paper' && userChoice === "rock") {
result = 'you lost!'
}
if(computerChoice === 'paper' && userChoice === "scissors") {
result = 'you won!'
}
if(computerChoice === 'scissors' && userChoice === "paper") {
result = 'you lost!'
}
if(computerChoice === 'scissors' && userChoice === "rock") {
result = 'you won!'
}
resultDisplay.innerHTML = result
}