-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
126 lines (96 loc) · 3.88 KB
/
script.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
118
119
120
121
122
123
124
125
126
let userScore = 0;
let computerScore = 0;
let draws = 0 ;
const drawsSpan = document.getElementById("draws");
const userScoreSpan = document.getElementById("user-score");
const computerScoreSpan = document.getElementById("computer-score");
const resultDiv = document.querySelector("#message");
const resetButton = document.getElementById("reset-button")
const rockDiv = document.getElementById("rock");
const paperDiv = document.getElementById("paper");
const scissorsDiv = document.getElementById("scissors");
const getComputerChoice = () => {
const choiceList = ["rock", "paper", "scissors"];
const randomNumber = Math.floor(Math.random()*3);
return (choiceList[randomNumber]);
};
const convertToUp = (word) => {
switch(word) {
case "rock":
return "Rock";
break;
case "paper":
return "Paper";
break;
case "scissors":
return "Scissors";
break;
}
};
const win = (userChoice, computerChoice) => {
userScore++;
userScoreSpan.innerHTML = userScore;
const randomWin = ["beats", "smashes", "destroys", "obliterates"];
const randomNumber = Math.floor(Math.random() * 4);
const winEmojis = ["🤠","🎉", "✨","🎊","🤩","👌"]
const randomNumberEmoji = Math.floor(Math.random() * 6);
resultDiv.innerHTML = `${convertToUp(userChoice)} ${randomWin[randomNumber]} ${convertToUp(computerChoice)}. You win! ${winEmojis[randomNumberEmoji]}`;
document.getElementById(userChoice).classList.add('win-border')
setTimeout(() => document.getElementById(userChoice).classList.remove('win-border'), 600);
};
const lose = (userChoice, computerChoice) => {
computerScore++;
computerScoreSpan.innerHTML = computerScore;
const randomWin = ["beats", "smashes", "destroys", "obliterates"];
const randomNumber = Math.floor(Math.random() * 4);
const loseEmojis = ["😩", "😥 ", "😭","😵💫","😔", "🤦🏽"]
const randomNumberEmoji = Math.floor(Math.random() * 6);
resultDiv.innerHTML = `${convertToUp(computerChoice)} ${randomWin[randomNumber]} ${convertToUp(userChoice)}. You lose! ${loseEmojis[randomNumberEmoji]}`;
document.getElementById(userChoice).classList.add('lose-border');
setTimeout(() => document.getElementById(userChoice).classList.remove('lose-border'), 600);
};
const tie = (userChoice, computerChoice) => {
draws++;
drawsSpan.innerHTML = draws ;
const tieEmojis = ["🤔", " 😱", "🙈", "🧐", "🙀", "🙃"];
const randomNumberEmoji = Math.floor(Math.random() * 6);
resultDiv.innerHTML = `${convertToUp(computerChoice)} matches ${convertToUp(userChoice)}. It's a tie! ${tieEmojis[randomNumberEmoji]}`;
document.getElementById(userChoice).classList.add('tie-border');
setTimeout(() => document.getElementById(userChoice).classList.remove('tie-border'), 600);
};
const 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":
tie(userChoice, computerChoice);
break;
}
};
const resetScores = () => {
computerScore = 0;
computerScoreSpan.innerHTML = computerScore
userScore = 0;
userScoreSpan.innerHTML = userScore;
draws = 0;
drawsSpan.innerHTML = draws ;
resultDiv.innerHTML = 'Who will win this match ?';
};
const main = () => {
rockDiv.addEventListener('click', () => game("rock"));
paperDiv.addEventListener('click', () => game("paper"));
scissorsDiv.addEventListener('click', () => game("scissors"));
resetButton.addEventListener('click', () => resetScores());
};
main();