-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-no-ui.js
73 lines (68 loc) · 1.81 KB
/
main-no-ui.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* main-no-ui.js Rock-Paper-Scissors Game
Barry White
3/11/21 */
function computerPlay() {
let num = Math.floor(Math.random() * 3) + 1;
switch (num) {
case 1:
return 'rock';
case 2:
return 'paper';
case 3:
return 'scissors';
}
}
function playerPlay() {
response = prompt('Enter Rock, Paper, Scissors:')
return response.toLowerCase();
}
function playRound(playerSelection, computerSelection) {
if (playerSelection.toLowerCase() == computerSelection) {
return 'Tie, play again.';
}
switch (playerSelection.toLowerCase()) {
case 'rock':
if (computerSelection == 'scissors') {
score++;
return 'You Win! Rock beats Scissors';
} else if (computerSelection == 'paper') {
score--;
return 'You Lose! Paper beats Rock';
}
break;
case 'paper':
if (computerSelection == 'rock') {
score++;
return 'You Win! Paper beats Rock';
} else if (computerSelection == 'scissors') {
score--;
return 'You Lose! Scissors beats Paper';
}
break;
case 'scissors':
if (computerSelection == 'paper') {
score++;
return 'You Win! Scissors beats Paper';
} else if (computerSelection == 'rock') {
score--;
return 'You Lose! Rock beats Scissors';
}
break;
}
}
function game() {
for (var i = 0; i < 5; i++) {
const playerSelection = playerPlay();
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
}
}
let score = 0;
game();
if (score > 0) {
console.log('You Won! -- Score:', score);
} else if (score < 0) {
console.log('You Lost! -- Score:', score);
} else {
console.log('Tie game, try again.');
}