-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
83 lines (75 loc) · 2.65 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<script>
function computerPlay(){
let pick = Math.floor(Math.random()*3);
if (pick == 0){
return("rock");
} else if (pick == 1){
return("paper");
} else if (pick == 2) {
return("scissors");
}
}
function playRound() {
let playerPickUnparsed = prompt("Type either: rock, paper, or scissors")
playerPick = stringToLowerCase(playerPickUnparsed);
let computerPick = computerPlay();
alert("The computer picked " + computerPick + "!");
if (playerPick == computerPick) {
return("It's a tie!")
} else if (playerPick == "rock"){
if (computerPick == "paper"){
return("You lose! Paper beats rock.");
} else if (computerPick == "scissors"){
return("You win! Rock beats scissors.");
}
} else if (playerPick == "paper"){
if (computerPick == "rock"){
return("You win! Paper beats rock.");
} else if (computerPick == "scissors"){
return("You lose! Scissors beats paper.");
}
} else if (playerPick == "scissors"){
if (computerPick == "paper"){
return("You win! Scissors beats paper.");
} else if (computerPick == "rock"){
return("You lose! Rock beats scissor.");
}
}
}
function game() {
let playerWins = 0;
let computerWins = 0;
let ties = 0;
let winner = "";
//play 5 rounds and keep track of score
for(round = 0; round < 5; round++){
alert("Round " + (round+1) + ", GO!");
roundMessage = playRound();
alert(roundMessage);
if (roundMessage.includes("win")){
playerWins +=1;
} else if (roundMessage.includes("lose")){
computerWins +=1;
} else {
ties +=1;
}
}
//compute and display winner
if (playerWins > computerWins) {
winner = "The player"
} else if (playerWins < computerWins){
winner = "The computer"
} else {
winner = "Nobody"
}
alert(winner + " wins! You had " + playerWins + " wins and the computer had " + computerWins + " wins.");
}
function stringToLowerCase(text) {
let lowerCased = "";
for (i = 0; i < text.length; i++){
lowerCased += text[i].toLowerCase();
}
return lowerCased;
}
game();
</script>