-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.js
117 lines (100 loc) · 3.46 KB
/
Function.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
let userScore = 0;
let computerScore = 0;
let userScore_span = document.getElementById("user-score");
let computerScore_span = document.getElementById("computer-score");
const scoreBoard_div = document.querySelector(".score-board");
const result_p = document.querySelector(".result > p");
const rock_div = document.getElementById("r");
const paper_div = document.getElementById("p");
const scissors_div = document.getElementById("s");
const action_p = document.getElementById("action-message");
const button_div = document.getElementById("btnn")
function getComputerChoice(){
const choices = ['r', 'p', 's'];
const randomNumber = Math.floor(Math.random() * 3);
return choices[randomNumber];
}
function convertToWord(letter){
if (letter === "r") return "Rock";
if (letter === "p") return "Paper";
return "Scissors";
}
function win(userChoice, computerChoice) {
userScore++;
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
result_p.innerHTML = `${convertToWord(userChoice)} beats ${convertToWord(computerChoice)} .you win`;
document.getElementById(userChoice).classList.add('green-glow');
setTimeout(() =>
document.getElementById(userChoice).classList.remove('green-glow')
, 300);
if(userScore >= 5 && computerScore < 5){
action_p.innerHTML = "Hurraah, You Won the Game";
rock_div.style.display = "none";
paper_div.style.display = "none";
scissors_div.style.display = "none";
result_p.style.display = "none";
button_div.style.display = "block";
}
}
function lose(userChoice, computerChoice) {
computerScore++;
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
result_p.innerHTML = `${convertToWord(userChoice)} loses to ${convertToWord(computerChoice)} .you lost`;
document.getElementById(userChoice).classList.add('red-glow');
setTimeout(() =>
document.getElementById(userChoice).classList.remove('red-glow')
, 300);
if (computerScore >= 5 && userScore < 5){
action_p.innerHTML = "Oh, You Just Lost";
rock_div.style.display = "none";
paper_div.style.display = "none";
scissors_div.style.display = "none";
result_p.style.display = "none";
button_div.style.display = "block";
}
}
function draw(userChoice, computerChoice) {
result_p.innerHTML = `${convertToWord(userChoice)} equals ${convertToWord(computerChoice)} .it's a draw`;
document.getElementById(userChoice).classList.add('gray-glow');
setTimeout(() =>
document.getElementById(userChoice).classList.remove('gray-glow')
, 300);
if (computerScore == 5 && userScore == 5){
action_p.innerHTML = "Oh, It's Just a Draw";
rock_div.style.display = "none";
paper_div.style.display = "none";
scissors_div.style.display = "none";
result_p.style.display = "none";
}
}
function game(userChoice){
const computerChoice = getComputerChoice();
switch(userChoice + computerChoice){
case "rs":
case "pr":
case "sp":
win(userChoice, computerChoice);
break;
case "rp":
case "ps":
case "sr":
lose(userChoice, computerChoice);
break;
case "rr":
case "pp":
case "ss":
draw(userChoice, computerChoice);
break;
}
}
function main(){
rock_div.addEventListener('click', () =>
game("r"));
paper_div.addEventListener('click', () =>
game("p"));
scissors_div.addEventListener('click', () =>
game("s"));
}
main();