-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRock_paper_scissors_V1.html
85 lines (74 loc) · 2.16 KB
/
Rock_paper_scissors_V1.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
84
85
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors</title>
</head>
<body>
<script>
//function that plays the role of the computer
function computerPlay() {
var result = Math.floor((Math.random() * 9) +1)
if(result >=1 && result<3)
{
return "rock"
}
else if (result>=3 && result<6) {
return "paper"
}
else {
return "scissors"
}
}
//To keep score
var computer = 0
var player = 0
function game()
{
//function to compute and declare the winner of a round
function playRound(computerSelection,playerSelection) {
playerSelection.toLowerCase()
if(computerSelection=="rock" && playerSelection =="paper")
{
player++
return "You win! Paper beats rock" + "\t You : " +player +"\t Computer: " +computer
}
else if (computerSelection == "paper" && playerSelection =="scissors")
{
player++
return "You win! Scissors beat paper" + "\t You : " +player +"\t Computer: " +computer;
}
else if (computerSelection =="scissors" && playerSelection =="rock")
{
player++
return "You win! Rock beats scissors" + "\t You : " +player +"\t Computer: " +computer;
}
else
{
computer++
return "Sorry! You lost to the computer,bro." + "\t You : " +player +"\t Computer: " +computer
}
}
//To play the game five times
for(var i=0; i<5; i++)
{
var computerSelection = computerPlay();
var playerSelection = prompt("rock, paper or scissors?");
console.log(playRound(computerSelection, playerSelection))
}
//To decide the final winner
if(computer>player)
{
console.log("You lose!")
}
else if (computer==player) {
console.log("It's a tie!")
}
else {
console.log("You win!")
}
}
game();
</script>
</body>
</html>