-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (56 loc) · 1.8 KB
/
script.js
File metadata and controls
58 lines (56 loc) · 1.8 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
46
47
48
49
50
51
52
53
54
55
56
57
58
function computerPlay() {
let decisionMaker;
decisionMaker = Math.floor(Math.random() * 3 + 1)
if (decisionMaker == 1) {
return "rock"
} else if (decisionMaker == 2) {
return "paper"
} else {
return "scissors"
}
}
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase()
console.log(`${playerSelection} vs ${computerSelection}`);
if (playerSelection === 'rock' && computerSelection === 'scissors') {
console.log('You win!');
return 'player';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
console.log('You lose!');
return 'computer';
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
console.log('You win!');
return 'player';
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
console.log('You lose!');
return 'computer';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
console.log('You win!');
return 'player';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
console.log('You lose!');
return 'computer'
} else if (playerSelection === computerSelection) {
console.log('It\'s a draw');
return 'draw';
} else {
console.log('Wrong input. You lose!');
return 'computer'
}
}
function game() {
let roundResult = 'nothing';
let playerChoice = 'nothing';
let playerScore = 0;
let computerScore = 0;
for (let i = 0; i < 5; i++) {
playerChoice = prompt('Rock, Paper, Scissors!');
roundResult = playRound(playerChoice, computerPlay());
if (roundResult === 'player') {
playerScore += 1
} else if (roundResult === 'computer') {
computerScore += 1
}
}
console.log(`Final score is ${playerScore}-${computerScore}`);
}