-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrps.js
More file actions
20 lines (17 loc) · 769 Bytes
/
rps.js
File metadata and controls
20 lines (17 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function playGame(playerChoice) {
const choices = ['rock', 'paper', 'scissors'];
const computerChoice = choices[Math.floor(Math.random() * choices.length)];
let result = '';
if (playerChoice === computerChoice) {
result = `It's a tie! You both chose ${playerChoice}.`;
} else if (
(playerChoice === 'rock' && computerChoice === 'scissors') ||
(playerChoice === 'paper' && computerChoice === 'rock') ||
(playerChoice === 'scissors' && computerChoice === 'paper')
) {
result = `You win! ${playerChoice} beats ${computerChoice}.`;
} else {
result = `You lose! ${computerChoice} beats ${playerChoice}.`;
}
document.getElementById('result').textContent = result;
}