-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (39 loc) · 1.19 KB
/
Copy pathindex.js
File metadata and controls
45 lines (39 loc) · 1.19 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
const choices = ['rock', 'paper', 'scissors'];
let cpuChoice = Math.floor(Math.random() * 3);
let playerChoice = prompt('Make your move!').toLowerCase();
if (playerChoice === choices) {
playerChoice === playerChoice;
}
function cpu(cpuChoice) {
if (cpuChoice === 0) {
return choices[0];
} else if (cpuChoice === 1) {
return choices[1];
} else {
return choices[2];
}
}
cpu(cpuChoice);
const win = 'You Win! 😁';
const lose = 'You lose! 😣';
const tie = 'Tie!';
function rpsGame(playerChoice) {
if (playerChoice === 'scissors' && cpu(cpuChoice) === 'rock') {
return lose;
} else if (playerChoice === 'rock' && cpu(cpuChoice) === 'scissors') {
return win;
} else if (playerChoice === 'paper' && cpu(cpuChoice) === 'rock') {
return win;
} else if (playerChoice === 'scissors' && cpu(cpuChoice) === 'paper') {
return win;
} else if (playerChoice === 'rock' && cpu(cpuChoice) === 'paper') {
return lose;
} else if (playerChoice === 'paper' && cpu(cpuChoice) === 'scissors') {
return lose;
} else {
return tie;
}
}
rpsGame(playerChoice);
document.getElementById('results').innerHTML = rpsGame(playerChoice);
document.getElementById('computerPlayed').innerHTML = cpu(cpuChoice);