-
Notifications
You must be signed in to change notification settings - Fork 0
/
promptScript.js
40 lines (36 loc) · 1.17 KB
/
promptScript.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
var a = ["Rock", "Paper", "Scissors"];
var playerPoints = 0;
var computerPoints = 0;
function computerPlay() {
var i = Math.floor(Math.random() * 3);
return a[i];
}
function playRound(playerSelection, computerSelection) {
if(playerSelection == computerSelection) {
return "Draw";
}
var win = playerSelection == "Rock" && computerSelection == "Scissors" || playerSelection == "Paper" && computerSelection == "Rock" || playerSelection == "Scissors" && computerSelection == "Paper";
if(win) {
playerPoints++;
return "You win! " + playerSelection + " beats " + computerSelection;
}
else {
computerPoints++;
return "You lose! " + computerSelection + " beats " + playerSelection;
}
}
function game() {
var set = "";
for(var i = 0; i < 5; i++) {
var player = prompt(set);
player = player.charAt(0).toUpperCase() + player.substr(1).toLowerCase();
set = playRound(player, computerPlay()) + "\nPlayer: " + playerPoints + " Computer: " + computerPoints;
}
if(playerPoints > computerPoints) {
console.log("You win!");
}
else {
console.log("You lose!");
}
}
game();