-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
109 lines (98 loc) · 3.1 KB
/
script.js
File metadata and controls
109 lines (98 loc) · 3.1 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
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
// Var declaration
let userChoice;
let compChoice;
let wins = 0;
let losses = 0;
// Node declaration
const rockBtn = document.getElementById("rockBtn");
const paperBtn = document.getElementById("paperBtn");
const scissorsBtn = document.getElementById("scissorsBtn");
const result = document.getElementById("result");
const score = document.getElementById("score");
// Get computer choice function
function getCompChoice() {
let num = Math.floor(Math.random() * 3) + 1;
let choice;
switch(num) {
case 1:
choice = "Rock"
break;
case 2:
choice = "Paper"
break;
case 3:
choice = "Scissors"
break;
}
return choice;
}
// Play round function
function playRound(userChoice, compChoice) {
if(userChoice == "rock" && compChoice == "Rock") {
result.textContent = "The computer chose Rock. It's a tie!";
return 0;
}
else if (userChoice == "rock" && compChoice == "Scissors") {
result.textContent = "The computer chose Scissors. Rock beats Scissors. You win!";
return 1;
}
else if (userChoice == "rock" && compChoice == "Paper") {
result.textContent = "The computer chose Paper. Paper beats Rock. You lose.";
return 2;
}
else if (userChoice == "paper" && compChoice == "Rock") {
result.textContent = "The computer chose Rock. Paper beats Rock. You win!";
return 1;
}
else if (userChoice == "paper" && compChoice == "Paper") {
result.textContent = "The computer chose Paper. It's a tie.";
return 0;
}
else if (userChoice == "paper" && compChoice == "Scissors") {
result.textContent = "The computer chose Scissors. Scissors beats paper. You lose.";
return 2;
}
else if (userChoice == "scissors" && compChoice == "Rock") {
result.textContent = "The computer chose Rock. Rock beats Scissors. You lose.";
return 2;
}
else if (userChoice == "scissors" && compChoice == "Paper") {
result.textContent = "The computer chose Paper. Scissors beats Paper. You win!";
return 1;
}
else if (userChoice == "scissors" && compChoice == "Scissors") {
result.textContent = "The computer chose Scissors. It's a tie.";
return 0;
}
}
// Function to keep score
function playMatch() {
switch(playRound(userChoice, compChoice)) {
case 0:
break;
case 1:
wins = wins + 1;
score.textContent = wins + " wins - " + losses + " losses";
break;
case 2:
losses = losses + 1;
score.textContent = wins + " wins - " + losses + " losses";
break;
}
}
// Button functionality
rockBtn.addEventListener("click", function(e) {
userChoice = "rock";
compChoice = getCompChoice();
playMatch();
});
paperBtn.addEventListener("click", function(e) {
userChoice = "paper";
compChoice = getCompChoice();
playMatch();
});
scissorsBtn.addEventListener("click", function(e) {
userChoice = "scissors";
compChoice = getCompChoice();
playMatch();
});