-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
77 lines (75 loc) · 2.41 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
function computerPlay() {
var randomNumber = Math.floor(Math.random() * (3)) + 1;
if (randomNumber == 1) {
return "Rock";
}
else if (randomNumber == 2) {
return "Paper";
}
else if (randomNumber == 3) {
return "Scissor";
}
}
var score = 0;
var computerScore = 0;
function playRound(playerSelection, computerSelection) {
// your code here!
if (playerSelection.toLowerCase() == "rock" && computerSelection == "Paper") {
computerScore += 1;
return "You lose! Paper beats Rock.";
}
else if (playerSelection.toLowerCase() == "paper" && computerSelection == "Rock") {
score += 1;
return "You win! Paper beats Rock.";
}
else if (playerSelection.toLowerCase() == "paper" && computerSelection == "Scissor") {
computerScore += 1;
return "You lose! Scissors beat Paper.";
}
else if (playerSelection.toLowerCase() == "scissor" && computerSelection == "Paper") {
score += 1;
return "You win! Scissors beat Paper.";
}
else if (playerSelection.toLowerCase() == "scissor" && computerSelection == "Rock") {
computerScore += 1;
return "You lose! Rock beats Scissor.";
}
else if (playerSelection.toLowerCase() == "rock" && computerSelection == "Scissor") {
score += 1;
return "You win! Rock beats Scissor.";
}
else if (playerSelection.toLowerCase() == computerSelection.toLowerCase()) {
return "Tie!";
}
return score, computerScore;
}
function winnerLoser() {
if (score > computerScore) {
return "Overall Score: Winner!"
}
else if (score == computerScore) {
return "Overall Score: Tie!"
}
else {
return "Overall Score: Loser!"
}
}
function game() {
i = 0;
while (i < 5) {
const playerSelection = window.prompt("Rock, paper, scissors!: ");
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
console.log("Your score is: " + score + ", The computer's score: " + computerScore);
i += 1;
}
return winnerLoser();
}
// For unlimited game (commented out):
// const userPrompt = window.prompt("Do you want to continue? Y/N: ")
// if (userPrompt == "Y") {
// continue;
// } else if (userPrompt == "N") {
// console.log("Your score is: " + score)
// break;
// }