-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
24 lines (21 loc) · 994 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const message = document.getElementById('message');
document.getElementById('rock').addEventListener('click', () => playGame('rock'));
document.getElementById('paper').addEventListener('click', () => playGame('paper'));
document.getElementById('scissors').addEventListener('click',() => playGame('scissors'));
function playGame(playerChoice){
const choice = ['rock', 'paper', 'scissors'];
const computerChoice = choice[Math.floor(Math.random()*3)];
if(playerChoice == computerChoice){
message.textContent = `it's a tie! Both chose ${playerChoice}.`;
}
else if(
(playerChoice === 'rock' && computerChoice === 'scissors') ||
(playerChoice === 'paper' && computerChoice === 'rock') ||
(playerChoice === 'scissors' && computerChoice === 'paper')
){
message.textContent = `You win! ${playerChoice} beats ${computerChoice}.`;
}
else{
message.textContent = `You lose! ${computerChoice} beats ${playerChoice}.`;
}
}