-
Notifications
You must be signed in to change notification settings - Fork 0
/
cricketgame.html
100 lines (89 loc) · 2.65 KB
/
cricketgame.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cricket Game</title>
<style>
body {
text-align: center;
}
</style>
</head>
<body>
<h1>Bat Ball Stump Game</h1>
<button
onclick="
// this will generate random number between zero and three.
generateComputerChoice();
resultMsg;
if(computerChoice ==='Ball'){
resultMsg='User Won';
}else if(computerChoice==='Bat'){
resultMsg=`It's a tie`;
}else if(computerChoice==='Stump'){
resultMsg='Computer Won';
}
alert(`you have chosen Bat. Computer choice is ${computerChoice} and ${resultMsg}`);
"
>
Bat
</button>
<button
onclick="
// this will generate random number between zero and three.
randomNum=Math.random()*3;
computerChoice;
generateComputerChoice();
resultMsg;
if(computerChoice ==='Ball'){
resultMsg=`It's a tie`;
}else if(computerChoice==='Bat'){
resultMsg='Computer Won';
}else if(computerChoice==='Stump'){
resultMsg='User Won';
}
alert(`you have chosen Ball. Computer choice is ${computerChoice} and ${resultMsg}`);
"
>
Ball
</button>
<button
onclick="// this will generate random number between zero and three.
randomNum=Math.random()*3;
computerChoice;
generateComputerChoice();
resultMsg;
if(computerChoice ==='Ball'){
resultMsg=`Computer Won`;
}else if(computerChoice==='Bat'){
resultMsg='User Won';
}else if(computerChoice==='Stump'){
resultMsg=`it's a tie`;
}
alert(`you have chosen Stump. Computer choice is ${computerChoice} and ${resultMsg}`);
"
>
Stamp
</button>
<!-- we can use cript tag and define variables in it to use the same variable in each button -->
<script>
let computerChoice;
let randomNum = Math.random() * 3;
let resultMsg;
function generateComputerChoice() {
if (randomNum > 0 && randomNum <= 1) {
computerChoice = "Bat";
// console.log('Computer choice is Bat');
} else if (randomNum > 1 && randomNum <= 2) {
computerChoice = "Ball";
// console.log('Computer choice is Ball');
} else {
computerChoice = "Stump";
// console.log('Computer choice is Stump');
}
}
</script>
<!-- !(Note:) we need to define function in script tAG TO MAKE it available for each button onclick action to work properly and we call that function to prove that action. -->
</body>
</html>