-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpsFunctions.js
More file actions
42 lines (36 loc) · 1.17 KB
/
rpsFunctions.js
File metadata and controls
42 lines (36 loc) · 1.17 KB
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
//Rock Paper Scissors game using functions
var hands = ['rock', 'paper', 'scissors'];
var player1Score = 0;
var player2Score = 0;
function getHand() {
return hands[parseInt(Math.random()*10)%3];
}
function playRound(player1, player2) {
var player1 = { name: 'Katy',
hand: getHand() };
var player2 = { name: 'Kim',
hand: getHand() };
if (player1.hand === player2.hand) {
console.log("It's a tie!")
}
else if ((player1.hand === 'rock' && player2.hand === 'scissors') ||
(player1.hand === 'scissors' && player2.hand === 'paper') ||
(player1.hand === 'paper' && player2.hand === 'rock')) {
player1Score += 1;
console.log(player1.name + " wins this round")
}
else {
console.log(player2Score += 1)
console.log(player2.name + " wins this round")
}
return (player1.name + ': ' + player1.hand + " vs " + player2.name + ': ' + player2.hand);
};
while (player1Score < 3 && player2Score < 3) {
if (player1Score == 3)
console.log("Katy wins the game!!")
// console.log(player1.name + " wins the game!!");
//player1.name not defined bc it is in playRound function
else
console.log("Kim wins the game!!");
console.log(playRound());
}