-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.html
67 lines (62 loc) · 1.71 KB
/
rps.html
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
<script>
function computerPlay() {
let num = Math.floor(Math.random() * 3);
if (num == 0) {
return "Rock";
}
else if (num == 1) {
return "Paper"
}
else if (num == 2) {
return "Scissors"
}
}
function playRound(player, cpu) {
let mine = player.toUpperCase();
let computer = cpu.toUpperCase();
if ((computer == "ROCK" && mine == "SCISSORS")
|| (computer == "PAPER" && mine == "ROCK")
|| (computer == "SCISSORS" && mine == "PAPER")) {
return "You Lose! " + computer + " beats " + mine;
}
else if ((computer == "PAPER" && mine == "SCISSORS")
|| (computer == "SCISSORS" && mine == "ROCK")
|| (computer == "ROCK" && mine == "PAPER")) {
return "You Win! " + mine + " beats " + computer;
}
else {
return "Draw! " + mine + " draws with " + computer;
}
}
function playGame() {
let playerscore = 0;
let computerscore = 0;
for (i = 0; i < 5; i++) {
let player = prompt("What do you choose?");
let computer = computerPlay();
let result = playRound(player, computer);
let exclaim = result.indexOf("!");
let compare = result.slice(0, exclaim);
if (compare == "You Lose") {
console.log("Computer wins round " + (i + 1));
computerscore += 1;
}
else if (compare == "You Win") {
console.log("You win round " + (i + 1));
playerscore += 1;
}
else {
console.log("You drew round " + (i + 1));
}
}
if (playerscore > computerscore) {
console.log("You win!");
}
else if (computerscore > playerscore) {
console.log("You lose!");
}
else {
console.log("You drew!");
}
}
</script>