-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathGuess-The-Number.html
More file actions
252 lines (215 loc) · 5.76 KB
/
Guess-The-Number.html
File metadata and controls
252 lines (215 loc) · 5.76 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Guess the number</title>
<style type="text/css">
body{
margin:0;
}
#reveal{
background-color: red;
color: white;
height: 50px;
margin-left: 50px;
}
#paragraph{
margin-left: 10px;
}
.compartment{
float: left;
margin-right: 50px;
}
##previousNumber{
margin-left: 50px;
}
#inputFieldContainer{
float: left;
margin-right: 20px;
}
#indicatorContainer{
float: left;
}
#generator{
background-color: purple;
color: white;
height: 50px;
margin-right: 10px;
}
#resetButton{
background-color: #88B04B;
color: white;
height: 50px;
}
#checkButton{
background-color: #C3447A;
color: white;
height: 50px;
}
#input{
height: 30px;
width: 100px;
border: 5px solid black;
}
#container{
height: 100%;
width: 98%;
position: relative;
border: 10px solid green;
}
#center{
padding-left: 5%;
padding-right: 5%
}
input{
width: 100px;
}
#left{
background-color: white;
width: 50px;
float: left;
margin-right: 20px;
height: 35px;
}
#right{
background-color: white;
width: 50px;
float: left;
height: 35px;
}
</style>
</head>
<body>
<div id="container">
<div id="center">
<h1>A Game: Guess The Number</h1>
<button id="generator" onclick="generator()" >Generate A Random Number</button>
<button id="resetButton" onclick="reset()">Reset</button>
<br>
<br>
<div class="compartment">
<div id="inputFieldContainer">
<input id="input" type="number" min="10" max="99" name="" placeholder="Enter number">
</div>
<div id="indicatorContainer">
<div id="left">.</div>
<div id="right">.</div>
</div>
<div class="compartment" id="#previousNumber">
<p id="paragraph">Previous Tries:</p>
</div>
</div>
<br>
<br>
<br>
<button id="checkButton" onclick="check()">Check</button>
<button id="reveal" onclick="show()">Reveal The Number</button>
<br>
<br>
<h4 id="tries">Tries:</h4>
<h2>Rules</h2>
<p>First generate a two digits random number, then guess your first try and check it.
<br>
Blue indicator means one of the number is correct irrespective of its place whereas green indicator means one number is correct as well as its position.
<br>
Analyze the indicators and your guessing over time to create your logic to get the number right in a least amount of tries.</p>
<h3>Copyright © Dev Kushal Pathak</h3>
</div>
</div>
<script type="text/javascript">
var counter = 0;
var leftColor = "white";
var rightColor = "white";
var random = undefined;
var randomLeft = 0;
var randomRight = 0;
var input = undefined;
var left = undefined;
var right = undefined;
var leftDiv = document.getElementById("left");
var rightDiv = document.getElementById("right");
var tries = document.getElementById("tries");
var paragraph = document.getElementById("paragraph");
var reveal = document.getElementById("reveal");
var isKnown = false;
var generatorButton = document.getElementById("generator");
function randomInt(min, max){
return(Math.floor(Math.random()*(max - min) + min));
}
function generator(){
random = randomInt(10,99);
randomLeft = Math.floor(random/10);
randomRight = random % 10;
generatorButton.style.border = "4px solid red";
if(randomLeft == randomRight){
generator();
}
//alert(random);
}
function reset(){
counter = 0;
leftColor = "white";
rightColor = "white";
random = undefined;
randomLeft = undefined;
randomRight = undefined;
input = undefined;
left = undefined;
right = undefined;
location.reload();
}
function show(){
reveal.innerHTML = random;
isKnown = true;
}
function check(){
leftColor = "white";
rightColor = "white";
if (counter >= 15) {
alert("You loose! maximum tries is 15");
}
counter++;
input = document.getElementById("input").value;
left = Math.floor(input/10);
right = input % 10;
if( left == randomLeft){
leftColor = "green";
}
if(right == randomRight && left != randomLeft){
leftColor = "green";
}
else if( right == randomRight && left == randomLeft){
leftColor = "green";
rightColor = "green";
}
if(left == randomRight && right != randomLeft){
leftColor = "blue";
}
else if(left != randomRight && right == randomLeft){
leftColor ="blue";
}
else if(left == randomRight && right == randomLeft){
leftColor = "blue";
rightColor = "blue";
}
leftDiv.style.backgroundColor = leftColor;
rightDiv.style.backgroundColor = rightColor;
tries.innerHTML = "Tries: " + counter;
paragraph.innerHTML += " " + input;
//alert(leftColor + " " + rightColor + " " + counter);
if (leftColor == "green" && rightColor == "green" && isKnown != true && counter == 1){
tries.innerHTML = "Congrats! you got it in " + counter + " try";
}
else if(leftColor == "green" && rightColor == "green" && isKnown == true && counter == 1){
tries.innerHTML = "You got it in " + counter + " try " + "by revealing the number :(";
}
else if (leftColor == "green" && rightColor == "green" && isKnown != true && counter > 1){
tries.innerHTML = "Congrats! you got it in " + counter + " tries";
}
else if(leftColor == "green" && rightColor == "green" && isKnown == true && counter > 1){
tries.innerHTML = "You got it in " + counter + " tries " + "by revealing the number :(";
}
}
</script>
</body>
</html>