-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (58 loc) · 2.4 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
console.log("hello");
// cache the DOM. instead of doing document commands every single time, we store it in a variable.
let userScore = 0;
let compScore = 0;
const userScore_span = document.getElementById("user-score");
const compScore_span = document.getElementById("comp-score");
const scoreBoard_div = document.querySelector(".score-board");
const result_p = document.querySelector(".result > p"); /* cos the text is in paragraph (<p>) */
const rock_div = document.getElementById("Rock");
const paper_div = document.getElementById("Paper");
const scissors_div = document.getElementById("Scissors");
function getComputerChoice(){
const choices = ["Rock","Paper","Scissors"];
const pick = Math.floor(Math.random() * 3);
return choices[pick];
}
function win(){
userScore++;
userScore_span.innerHTML = userScore;
}
function lose(){
compScore++;
compScore_span.innerHTML = compScore;
}
function game(userChoice) {
const compChoice = getComputerChoice();
const combine = userChoice + compChoice;
switch(combine) {
case "RockRock":
case "PaperPaper":
case "ScissorsScissors":
result_p.innerHTML = userChoice + " is the same as " + compChoice + ". Draw!!";
document.getElementById(userChoice).classList.add('grey-glow');
setTimeout(() => {document.getElementById(userChoice).classList.remove('grey-glow')}, 300);
break;
case "RockScissors":
case "PaperRock":
case "ScissorsPaper":
win();
result_p.innerHTML = userChoice + " beats " + compChoice + ". You win!!";
document.getElementById(userChoice).classList.add('green-glow');
setTimeout(() => {document.getElementById(userChoice).classList.remove('green-glow')}, 300);
break;
case "RockPaper":
case "PaperScissors":
case "ScissorsRock":
lose();
result_p.innerHTML = userChoice + " loses to " + compChoice + ". You lost!!";
document.getElementById(userChoice).classList.add('red-glow');
setTimeout(() => {document.getElementById(userChoice).classList.remove('red-glow')}, 300);
break;
}
console.log(compChoice);
}
// clicking buttons
rock_div.addEventListener('click', () => game("Rock"));
paper_div.addEventListener('click', () => game("Paper"));
scissors_div.addEventListener('click', () => game("Scissors"));