-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (67 loc) · 2.63 KB
/
Copy pathscript.js
File metadata and controls
80 lines (67 loc) · 2.63 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
74
75
76
77
78
79
80
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 smallUserword = "U".fontsize(3).sup();
const smallCompword = "C".fontsize(3).sup();
function getComputerChoice(){
const choices = ['Rock', 'Paper', 'Scissors'];
const randomNumber = (Math.floor(Math.random() * 3));
return choices[randomNumber];
}
function getImg(C) {
switch (C) {
case "Rock":
return '<img src="https://www.rpsgame.org/assets/img/rock.svg" alt="" height="80" width="80" >'
case "Paper":
return '<img src="https://www.rpsgame.org/assets/img/paper.svg" alt="" height="80" width="80" >'
case "Scissors":
return '<img src="https://www.rpsgame.org/assets/img/scissors.svg" alt="" height="80" width="80" >'
}
}
function win(userChoice, computerChoice) {
userScore++;
result_div.innerHTML = getImg(userChoice) + ` - ${cimg} <br> WIN ➕`;
}
function lose(userChoice, computerChoice) {
computerScore++;
result_div.innerHTML = getImg(userChoice) + ` - ${cimg} <br> LOSE ➖`;
}
function draw(userChoice, computerChoice) {
result_div.innerHTML = getImg(userChoice) + ` - ${cimg} <br> DRAW 🚫`;
}
function game(userChoice) {
const computerChoice = getComputerChoice();
uimg = getImg(userChoice);
cimg = getImg(computerChoice);
switch (userChoice + computerChoice) {
case "RockScissors":
case "PaperRock":
case "ScissorsPaper":
win(userChoice, computerChoice)
break;
case "RockPaper":
case "PaperScissors":
case "ScissorsRock":
lose(userChoice, computerChoice)
break;
default:
draw(userChoice, computerChoice)
break;
}
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
document.getElementById(userChoice).classList.add('red-glow');
setTimeout(() => document.getElementById(userChoice).classList.remove('red-glow'), 300);
}
function main(){
rock_div.addEventListener('click', () => game("Rock"));
paper_div.addEventListener('click', () => game("Paper"));
scissors_div.addEventListener('click', () => game("Scissors"));
}
main();