-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (78 loc) · 3.23 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
let userScore=0;
let computerScore=0;
const userScore_span=document.getElementById("user-score");
const computerScore_span=document.getElementById("computer-score");
const scoreBoard_div=document.querySelector(".score-board");
const result_div=document.querySelector(".result > p");
const rock_div=document.getElementById("Rock");
const paper_div=document.getElementById("Paper");
const scissors_div=document.getElementById("Scissors");
const resetButton = document.getElementById("reset-button")
function getComputerChoice(){
const choices=["Rock","Paper","Scissors"];
const randomNumber=Math.floor(Math.random()*3);
return choices[randomNumber];
}
function resetScores(){
computerScore = 0;
computerScore_span.innerHTML = computerScore
userScore = 0;
userScore_span.innerHTML = userScore;
};
function win(userChoice, computerChoice){
const smallUserWord="user".fontsize(3).substring();
const smallComputerWord="computer".fontsize(3).substring();
const userChoice_div=document.getElementById(userChoice);
userScore++;
userScore_span.innerHTML=userScore;
computerScore_span.innerHTML=computerScore;
result_div.innerHTML=`${userChoice} ${smallUserWord} beats ${computerChoice} ${smallComputerWord} . You win! 🔥 `;
userChoice_div.classList.add('green-glow');
setTimeout(() => userChoice_div.classList.remove('green-glow'),500);
}
function lose(userChoice, computerChoice){
const smallUserWord="user".fontsize(3).substring();
const smallComputerWord="computer".fontsize(3).substring();
const userChoice_div=document.getElementById(userChoice);
computerScore++;
computerScore_span.innerHTML=computerScore;
userScore_span.innerHTML=userScore;
result_div.innerHTML=`${userChoice} ${smallUserWord} loses to ${computerChoice} ${smallComputerWord} . You lost... 🤦`;
userChoice_div.classList.add('red-glow');
setTimeout(() => userChoice_div.classList.remove('red-glow'),500);
}
function draw(userChoice, computerChoice){
const smallUserWord="user".fontsize(3).substring();
const smallComputerWord="computer".fontsize(3).substring();
const userChoice_div=document.getElementById(userChoice);
result_div.innerHTML=`${userChoice} ${smallUserWord} equals ${computerChoice} ${smallComputerWord} . It's a draw 🙃`;
userChoice_div.classList.add('gray-glow');
setTimeout(() => userChoice_div.classList.remove('gray-glow'),500);
}
function game(userChoice){
const computerChoice=getComputerChoice();
switch (userChoice + computerChoice) {
case "PaperRock":
case "RockScissors":
case "ScissorsPaper":
win(userChoice, computerChoice);
break;
case "RockPaper":
case "ScissorsRock":
case "PaperScissors":
lose(userChoice, computerChoice);
break;
case "RockRock":
case "PaperPaper":
case "ScissorsScissors":
draw(userChoice, computerChoice);
break;
}
}
function main(){
rock_div.addEventListener('click',() => game("Rock"));
paper_div.addEventListener('click',() => game("Paper"));
scissors_div.addEventListener('click',() => game("Scissors"));
resetButton.addEventListener('click', () => resetScores());
}
main();