-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexta.html
208 lines (202 loc) · 5.77 KB
/
exta.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>extra</title>
<!-- Google Font -->
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
rel="stylesheet"
/>
<!-- Stylesheet -->
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background-color: #3c6fff;
}
.container {
width: min(90%, 31.25em);
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
background-color: #ffffff;
padding: 4em 2em;
border-radius: 0.5em;
box-shadow: -1.5em -1.5em #202020;
text-align: center;
}
h3 {
font-size: 1.2em;
font-weight: 500;
line-height: 2em;
color: #303030;
}
.input-wrapper {
width: 70%;
display: grid;
grid-template-columns: 2fr 3fr;
gap: 1em;
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;
appearance: text-field;
-moz-appearance: textfield;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type="number"]:focus {
border-color: #3c6fff;
}
button {
font-size: 1em;
background-color: #3c6fff;
color: #ffffff;
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;
}
.result {
margin-top: 1em;
}
p {
font-size: 1em;
font-weight: 400;
color: #565b70;
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>
</head>
<body>
<div class="container">
<div id="game">
<h3>
I am thinking of a number between 1-100.<br />
Can you guess it?
</h3>
<div class="input-wrapper">
<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"></div>
</div>
</div>
<!-- Script -->
<script>
const hint = document.getElementById("hint");
const noOfGuessesRef = document.getElementById("no-of-guesses");
const guessedNumsRef = document.getElementById("guessed-nums");
const restartButton = document.getElementById("restart");
const game = document.getElementById("game");
const guessInput = document.getElementById("guess");
const checkButton = document.getElementById("check-btn");
let answer, noOfGuesses, guessedNumsArr;
const play = () => {
const userGuess = guessInput.value;
if (userGuess < 1 || userGuess > 100 || isNaN(userGuess)) {
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!";
} else {
hint.innerHTML = "Too high. Try Again!";
}
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 {
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";
}
};
const init = () => {
console.log("Game Started");
answer = Math.floor(Math.random() * 100) + 1;
console.log(answer);
noOfGuesses = 0;
guessedNumsArr = [];
noOfGuessesRef.innerHTML = "No. Of Guesses: 0";
guessedNumsRef.innerHTML = "Guessed Numbers are: None";
guessInput.value = "";
hint.classList.remove("success", "error");
};
guessInput.addEventListener("keydown", (event) => {
if (event.keyCode === 13) {
event.preventDefault();
play();
}
});
restartButton.addEventListener("click", () => {
game.style.display = "grid";
restartButton.style.display = "none";
hint.innerHTML = "";
hint.classList.remove("success");
init();
});
checkButton.addEventListener("click", play);
window.addEventListener("load", init);
</script>
</body>
</html>