-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (52 loc) · 1.58 KB
/
Copy pathscript.js
File metadata and controls
66 lines (52 loc) · 1.58 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
console.log("Hello World");
let rock = document.getElementById("rock");
let paper = document.getElementById("paper");
let scissors = document.getElementById("scissors");
let scoreboard = document.getElementById("score");
let result = document.getElementById("winMessage");
function getCompChoice() {
let robot = Math.floor(Math.random() * 3);
switch (robot) {
case 0:
return "Rock";
case 1:
return "Paper";
case 2:
return "Scissors";
}
}
function rockChoice() P
playRound("Rock");
function paperChoice() {
playRound("Paper");
}
function scissorsChoice() {
playRound("Scissors");
}
let humanWins = 0;
let robotWins = 0;
function playRound(humanChoice) {
let robotChoice = getCompChoice();
if (
(humanChoice == "Rock" && robotChoice == "Scissors") ||
(humanChoice == "Scissors" && robotChoice == "Paper") ||
(humanChoice == "Paper" && robotChoice == "Rock")
) {
result.innerHTML = `${humanChoice} beats ${robotChoice}! You Win!`;
humanWins++;
scoreboard.innerHTML = `${humanWins} : ${robotWins}`;
} else if (
(robotChoice == "Rock" && humanChoice == "Scissors") ||
(robotChoice == "Scissors" && humanChoice == "Paper") ||
(robotChoice == "Paper" && humanChoice == "Rock")
) {
result.innerHTML = `${robotChoice} beats ${humanChoice}! You lose...`;
robotWins++;
scoreboard.innerHTML = `${humanWins} : ${robotWins}`;
} else {
result.innerHTML = "It's a tie!"
}
}
rock.addEventListener("click", rockChoice);
paper.addEventListener("click", paperChoice);
scissors.addEventListener("click", scissorsChoice);