-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (39 loc) · 1.19 KB
/
index.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const choices = ['rock', 'paper', 'scissors']
let playerScore = 0
let computerScore = 0
function getComputerChoice (arr) {
return arr[Math.floor(Math.random() * arr.length)]
}
function playRound (playerSelection, computerSelection) {
if (
(playerSelection === 'scissors' && computerSelection === 'paper') ||
(playerSelection === 'rock' && computerSelection === 'scissors') ||
(playerSelection === 'paper' && computerSelection === 'rock')
) {
playerScore++
return `You win ! ${playerSelection} beats ${computerSelection}`
} else if (playerSelection === computerSelection) {
return "it's a tie"
} else {
computerScore++
return `You Lose ! ${computerSelection} beats ${playerSelection}`
}
}
function game () {
for (let i = 1; i < 6; i++) {
const playerSelection = prompt(
'Choose an option',
'Rock,Paper or scissors'
).toLocaleLowerCase()
const computerSelection = getComputerChoice(choices)
console.log(i, playRound(playerSelection, computerSelection))
}
if (playerScore > computerScore) {
return 'You won !'
} else if (computerScore > playerScore) {
return 'You lost'
} else {
return 'You tied'
}
}
console.log(game())