-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
54 lines (48 loc) · 1.77 KB
/
main.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
function computerPlay() {
let val = ['Rock','Paper','Scissor'];
let play = val[Math.floor(Math.random() * val.length)];
return play.toLowerCase();
}
function playRound(playerSelection, computerSelection) {
console.log('You: ' + playerSelection + ', ' + 'Computer: ' + computerSelection);
if (playerSelection == computerSelection) console.log("Tie!");
else if (playerSelection == 'rock' && computerSelection == 'scissor') {
user ++;
return("You win! Rock beats Scissor");
}
else if (playerSelection == 'rock' && computerSelection == 'paper') {
computer ++ ;
return("You lose! Paper beats Rock");
}
else if (playerSelection == 'scissor' && computerSelection == 'paper') {
user ++ ;
return("You win! Scissor beats Paper");
}
else if (playerSelection == 'scissor' && computerSelection == 'rock') {
computer ++;
return("You lose! Rock beats Scissor");
}
else if (playerSelection == 'paper' && computerSelection == 'rock') {
user ++;
return("You win! Paper beats Rock");
}
else if (playerSelection == 'paper' && computerSelection == 'scissor') {
computer++;
return("You lose! Scissor beats Paper");
}
else return('Please enter either Rock,Paper,Scissor');
}
let computer = 0
let user = 0
function game() {
for (let i=1; i <= 5; i++) {
if (computer == 5 || user == 5) break;
console.log(playRound(prompt("RPS GAME", "Please enter your play").toLowerCase(),computerPlay()));
i = 0;
}
console.log('Computer: ' + computer, 'You: ' + user);
if (computer == user) return('Tie!')
else if (computer > user) return('You lose!');
else if (user > computer) return('You win!');
}
console.log(game())