-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
51 lines (43 loc) · 1.15 KB
/
Game.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
// Paper and scissor
const play = {
1: "paper",
2: "scissor",
3: "rock",
};
function getFromComputer() {
let compChoice = Math.floor(Math.random() * 3) + 1;
return play[compChoice];
}
function playRound(playerSelection, computerSelection) {
let play1 = playerSelection;
let play2 = computerSelection;
if (play1 == "rock" && play2 == "paper") {
return "You lost";
} else if (play1 == "rock" && play2 == "scissor") {
return "You won";
} else {
return "It's null";
}
}
function playGame() {
let a = 0;
let b = 0;
for (let i = 0; i < 5; i++) {
const playerSelection = prompt("Enter your choice");
const computerSelection = getFromComputer();
let ans = playRound(playerSelection, computerSelection);
if (ans == "You lost") {
a++;
} else if (ans == "You won") {
b++;
}
}
if (a < b) {
console.log("You won");
} else if (a > b) {
console.log("The computer won!");
} else {
console.log("The game is null, no one won!");
}
}
playGame();