-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
85 lines (68 loc) · 1.98 KB
/
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
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
74
75
76
77
78
79
80
81
82
83
let playerScore = 0;
let computerScore = 0;
function computerPlay() {
let random = Math.floor(Math.random()*(3-1+1))+1;
if (random == 1) {
return 'ROCK'
} else if (random == 2) {
return 'PAPER'
} else {
return 'SCISSORS'
}
}
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toUpperCase();
if (playerSelection === computerSelection) {
return 'Its a Draw!';
} else if (
(playerSelection === 'PAPER' && computerSelection === 'SCISSORS') ||
(playerSelection === 'ROCK' && computerSelection === 'PAPER') ||
(playerSelection === 'SCISSORS' && computerSelection === 'ROCK')
) {
computerScore = ++computerScore;
return 'You lose! ' + playerSelection + ' loses against ' + computerSelection;
} else {
playerScore = ++playerScore;
return 'You Win! ' + playerSelection + ' wins against ' + computerSelection;
}
}
function game(){
for (let i = 0; i < 5; i++){
playerSelection = prompt();
playerSelection = playerSelection.toUpperCase();
computerSelection = computerPlay();
playRound(playerSelection, computerSelection)
console.log(playerSelection);
console.log(computerSelection);
console.log(playerScore);
console.log(computerScore);
}
console.log(gameWin());
gameReset();
}
function gameWin(){
if (playerScore > computerScore) {
return 'Congratulations, You won the game!'
} else if (playerScore < computerScore) {
return 'The Machines have won, better luck next time.'
} else {
return 'Its a tie'
}
}
function gameReset() {
var reset = prompt('Do you Want to play again?')
switch(reset) {
case "yes":
scoreReset();
game();
case "no":
alert("Hope you had fun!");
}
}
function scoreReset() {
playerScore = 0;
computerScore = 0;
}
console.log(game());
console.log(playerScore);
console.log(computerScore);