-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (37 loc) · 1.12 KB
/
script.js
File metadata and controls
41 lines (37 loc) · 1.12 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
function getChoices(playerChoice) {
const options = ['rock', 'paper', 'scissors'];
const computerChoice = options[Math.floor(Math.random() * 3)];
return {
player: playerChoice,
computer: computerChoice
};
}
function whoWon(player, computer) {
if (player === computer) {
return "tie";
} else if (player === "rock") {
if (computer === "scissors") {
return "you win";
} else {
return "you lose";
}
} else if (player === "paper") {
if (computer === "scissors") {
return "you lose";
} else {
return "you win";
}
} else if (player === "scissors") {
if (computer === "rock") {
return "you lose";
} else {
return "you win";
}
}
}
function play(playerChoice) {
const choices = getChoices(playerChoice);
const result = whoWon(choices.player, choices.computer);
document.getElementById("result").innerText =
`You chose ${choices.player}, computer chose ${choices.computer} → ${result}`;
}