-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (60 loc) · 1.65 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
62
63
64
const resultDisplay = document.getElementById("result");
const userChoiceDisplay = document.getElementById("user-choice");
const computerChoiceDisplay = document.getElementById("computer-choice");
const possibleChoices = document.querySelectorAll("button");
let userChoice;
let computerChoice;
let result;
possibleChoices.forEach((possibleChoice) =>
possibleChoice.addEventListener("click", (e) => {
userChoice = e.target.id;
userChoiceDisplay.innerHTML = userChoice;
generateComputerChoice();
getResult();
})
);
function generateComputerChoice() {
const randomNumber = Math.floor(Math.random() * possibleChoices.length)+1;
console.log(randomNumber);
if (randomNumber === 1) {
computerChoice = "paper";
}
if (randomNumber === 2) {
computerChoice = "rock";
}
if (randomNumber === 3) {
computerChoice = "scissors";
}
computerChoiceDisplay.innerHTML = computerChoice;
}
function getResult () {
if (computerChoice === userChoice)
{
result = 'its a draw!'
}
if (computerChoice === 'rock' && userChoice ==='paper')
{
result = 'you win!'
}
if (computerChoice === 'rock' && userChoice ==='scissors')
{
result = 'you lost!'
}
if (computerChoice === 'scissors' && userChoice ==='paper')
{
result = 'you lost!'
}
if (computerChoice === 'scissors' && userChoice ==='rock')
{
result = 'you win!'
}
if (computerChoice === 'paper' && userChoice ==='scissors')
{
result = 'you win!'
}
if (computerChoice === 'paper' && userChoice ==='rock')
{
result = 'you lost!'
}
resultDisplay.innerHTML = result;
}