-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
74 lines (67 loc) · 2.34 KB
/
app.js
File metadata and controls
74 lines (67 loc) · 2.34 KB
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
let userChosen;
let computerChosen;
var result = results();
const computerChoice = document.getElementById('computer-choice');
const userChoice = document.getElementById('user-choice');
let randomNumber;
const possibleChoices = document.querySelectorAll('.choices');
//get userChosen
possibleChoices.forEach(possibleChoice => possibleChoice.addEventListener('click',(e) => {
userChosen = e.target.id;
randomNumber = Math.floor(Math.random()*(4));
generatedComputerChoice();
results();
userChoice.innerHTML = userChosen;
computerChoice.innerHTML = computerChosen;
if(result ==='you lost'){
document.getElementById('lose').style.display="inline-flex";
document.getElementById('tied').style.display="none";
document.getElementById('win').style.display='none';
}
else if(result === 'There was a tie'){
document.getElementById('tied').style.display="inline-flex";
document.getElementById('win').style.display='none';
document.getElementById('lose').style.display="none";
}
else if(result === 'you win!'){
document.getElementById('win').style.display='inline-flex';
document.getElementById('lose').style.display="none";
document.getElementById('tied').style.display="none";
}
}));
//get a random computer choice
function generatedComputerChoice(){
if(randomNumber === 1){
return computerChosen = "rock";
}
else if(randomNumber === 2){
return computerChosen = "paper";
}
else if(randomNumber === 3){
return computerChosen = "scissors";
}
}
//get Results
function results() {
if(computerChosen === userChosen){
return result = "There was a tie";
}
else if(computerChosen === 'rock' && userChosen === 'paper'){
return result = 'you win!';
}
else if(computerChosen === 'rock' && userChosen === 'scissors'){
return result='you lost';
}
else if(computerChosen === 'paper' && userChosen === 'rock'){
return result = 'you lost';
}
else if(computerChosen === 'paper' && userChosen === 'scissors'){
return result = 'you win!';
}
else if(computerChosen === 'scissors' && userChosen === 'paper'){
return result = 'you lost';
}
else if(computerChosen === 'scissors' && userChosen === 'rock'){
return result = 'you win!';
}
}