-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuess the number.html
255 lines (234 loc) · 6.36 KB
/
Guess the number.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
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
253
254
255
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Guess the number</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous"
/>
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
rel="stylesheet"
/>
</head>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background-color: lavender;
}
h1 {
color: teal;
}
.box {
border-radius: 10px;
padding: 4rem 3.2rem;
width: min(90%, 31.25em);
position: absolute;
top: 45%;
left: 50%;
transform: translate(-50%, -50%);
background-color: teal;
color: white;
box-shadow: 0 0 15px rgb(0, 0, 0);
text-align: center;
}
h3 {
color: white;
font-size: 1.2em;
font-weight: 500;
line-height: 2em;
}
.input-wrapper {
width: 70%;
display: grid;
grid-template-columns: 2fr 3fr;
gap: 16px;
margin: 3em auto 1.5em auto;
}
input[type="number"] {
width: 100%;
padding: 1em;
font-size: 1.2em;
text-align: center;
border: 2px solid #adaeae;
border-radius: 0.3em;
outline: none;
background-color: lavender;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type="number"]:focus {
border-color: black;
}
button {
font-size: 20px;
background-color: lavender;
color: teal;
border: none;
outline: none;
border-radius: 0.5em;
font-weight: 500;
cursor: pointer;
}
#restart {
margin: 0 auto 0 auto;
padding: 0.8em 1em;
display: none;
}
p {
font-size: 1em;
font-weight: 400;
color: white;
word-break: break-all;
}
.error,
.success {
padding: 0.5em 0;
border-radius: 0.3em;
margin-bottom: 1em;
animation: pop 0.4s forwards;
transform: scale(0);
}
.error {
background-color: #ffcbcb;
color: #ff3e3e;
}
.success {
background-color: #b9ffd5;
color: #05c451;
}
@keyframes pop {
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
</style>
<body>
<div class="container mt-5">
<h1>Guess The Number</h1>
<div class="box">
<div class="game" id="game">
<h3>
I am thinking of a number between 1-100.<br />
Can you guess it?
</h3>
<div class="input-wrapper mb-5">
<input type="number" placeholder="00" id="guess" />
<button id="check-btn">Guess</button>
</div>
<p id="no-of-guesses">No. Of Guesses: 0</p>
<p id="guessed-nums">Guessed Numbers are: None</p>
</div>
<button id="restart">Restart</button>
<div class="result">
<div id="hint" class="mt-3"></div>
</div>
</div>
</div>
</body>
<script>
// DOM
const game = document.getElementById("game");
const guessInput = document.getElementById("guess");
const checkButton = document.getElementById("check-btn");
const noOfGuessesRef = document.getElementById("no-of-guesses");
const guessedNumsRef = document.getElementById("guessed-nums");
const restartButton = document.getElementById("restart");
const hint = document.getElementById("hint");
let answer, noOfGuesses, guessedNumsArr;
console.log("game", game);
console.log("guessInput", guessInput);
console.log("checkButton", checkButton);
console.log("noOfGuessesRef", noOfGuessesRef);
console.log("guessedNumsRef", guessedNumsRef);
console.log("restartButton", restartButton);
console.log("hint", hint);
//Main Logic
const play = () => {
const userGuess = guessInput.value;
console.log("Userguess", userGuess);
if (userGuess < 1 || userGuess > 100) {
alert("Please enter a valid number between 1 and 100.");
return;
}
guessedNumsArr.push(userGuess);
noOfGuesses += 1;
if (userGuess != answer) {
if (userGuess < answer) {
hint.innerHTML = "Too low. Try Again!";
console.log("Too low");
} else {
hint.innerHTML = "Too high. Try Again!";
console.log("Too high");
}
noOfGuessesRef.innerHTML = `<span>No. Of Guesses:</span> ${noOfGuesses}`;
guessedNumsRef.innerHTML = `<span>Guessed Numbers are:</span> ${guessedNumsArr.join(
" | "
)}`;
hint.classList.remove("error");
setTimeout(() => {
hint.classList.add("error");
}, 10);
} else {
console.log("currect guess");
hint.innerHTML = `Congratulations! <br>
The Number Was <span>${answer}</span>.
<br> You guessed the number in <span>${noOfGuesses} </span>tries.`;
hint.classList.add("success");
game.style.display = "none";
restartButton.style.display = "block";
}
};
//Random generate number
const init = () => {
console.warn("game start");
answer = Math.floor(Math.random() * 100);
console.log("random no.", answer);
noOfGuesses = 0;
guessedNumsArr = [];
guessInput.value = "";
hint.classList.remove("success", "error");
};
//input logic
guessInput.addEventListener("keydown", (ele) => {
// console.log("Input-Numbers");
if (ele.keyCode === 13) {
ele.preventDefault();
play();
}
});
// check button
checkButton.addEventListener("click", () => {
console.log("Check number");
play();
});
//restart button
restartButton.addEventListener("click", () => {
game.style.display = "grid";
restartButton.style.display = "none";
hint.innerHTML = "";
hint.classList.remove("success");
init();
guessInput.focus();
});
window.addEventListener("load", () => {
init();
guessInput.focus();
});
</script>
</html>