-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
288 lines (283 loc) · 8.66 KB
/
index.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
* {
font-family: Arial, Helvetica, sans-serif;
--color-dark: #2b2d42;
--color-mid: #ffffff10;
--color-light: #edf2f4;
--color-a-light: #ef233c;
--color-a-dark: #d90429;
color: var(--color-light);
}
h1, h3 {
font-family: Impact, Charcoal, sans-serif;
}
hr {
color: var(--color-mid);
}
body {
margin: 0;
padding: 1em;
min-width: calc(100vw - 3em - 1px);
min-height: calc(100vh - 2em);
display: flex;
flex-direction: row;
align-items: stretch;
justify-content: center;
background-color: var(--color-dark);
}
.container {
width: 100%;
max-width: 1080px;
padding: 1em;
background-color: var(--color-mid);
border-radius: 1em;
}
button {
border: none;
background-color: var(--color-a-light);
padding: 1em 2em;
border-radius: 0.3em;
}
input {
padding: 0.5em;
border-radius: 0.3em;
font-size: 1.25em;
border: none;
color: var(--color-dark);
}
.bumpRight {
margin-left: 1em;
}
.inputHolder {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
margin-bottom: 1em;
}
.testButton {
padding: 0.5em 1em;
}
</style>
</head>
<body>
<div class="container">
<h1>Guess the Number!</h1>
<p>Can you think of the number the computer is guessing?</p>
<hr>
<h3>How to Play</h3>
<ol>
<li>
<p>Press on the 'Play' button to begin the game.</p>
</li>
<li>
<p>The computer will tell you it is thinking of a number.</p>
</li>
<li>
<p>Enter a number into the box, and click 'Submit' to check your guess.</p>
<ul>
<li>
<p>If you guessed correct, the computer will tell you.</p>
</li>
<li>
<p>If not, the computer will tell you to guess <b>higher</b> or <b>lower</b>.</p>
</li>
</ul>
</li>
<li>
<p>Guess numbers until you win! Or press 'New Game' to start a new game if you don't want to play this one.</p>
</li>
<li>
<p>Play again by pressing 'New Game', because this is obviously the best game you've ever played! :)</p>
</li>
</ol>
<hr>
<div id="main" style="display: none;">
<p>I'm thinking of an integer between <span id="lower">1</span> and <span id="upper">100</span> (inclusive).</p>
<p id="prompt" style="display: none;"></p>
<div class="inputHolder">
<input id="inp" type="number">
<button id="submit" class="bumpRight">Submit</button>
</div>
</div>
<button id="play" step="1">Play</button>
<div id="mainTest" style="display: none;">
<br>
<br>
<br>
<button id="test" class="testButton">Test me!</button>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script type="text/javascript">
function testValidateInput(fxn, high, low) {
// test the fxn function, with bounds high and low.
console.group("Testing the isValidInput function:", fxn);
if (typeof fxn !== 'function') {
console.warn('[WARNING]: Value supplied is not a function! Cannot proceed with tests.');
} else {
console.info("Starting tests...");
const testArr = [
{ in: Math.round(Math.random() * (high - low) + low), ex: true },
{ in: Math.round(Math.random() * (high - low) + low), ex: true },
{ in: Math.round(Math.random() * (high - low) + low), ex: true },
{ in: Math.round(Math.random() * (high - low) + low), ex: true },
{ in: Math.round(Math.random() * (high - low) + low), ex: true },
{ in: false, ex: false },
{ in: 3.1415926535, ex: false },
{ in: "Test me", ex: false },
{ in: [1,2,3], ex: false },
{ in: {}, ex: false },
{ in: null, ex: false },
{ in: undefined, ex: false },
];
let total = 0, passed = 0, failed = 0;
testArr.forEach((test, i) => {
console.log("Test", i, "| Input:", test.in, "; Expected Output:", test.ex);
let out;
try {
out = fxn(test.in);
} catch(e) {
console.warn('[WARNING]: test crashed!', test);
console.warn('Error reported is:', e);
out = false;
}
const didPass = !!(out === test.ex);
if (didPass) {
passed++;
console.log("Actual Output:", out, "| Passed!");
} else {
failed++;
console.error("Actual Output:", out, "| Failed!");
}
total++;
});
console.info(`Testing complete. Ran ${total} tests in total. Results:`);
console.log(`Passed: ${passed}/${total} (${(passed/total).toFixed(2)}%)`);
console.log(`Failed: ${failed}/${total} (${(failed/total).toFixed(2)}%)`);
if (passed === total) {
console.info("All tests passed!");
} else {
console.warn("[WARNING]: Not all tests passed!");
}
}
console.groupEnd();
}
$(document).ready(() => {
console.log('Ready!');
var showAlert = true;
var number;
var upperBound = 100;
var lowerBound = 0;
const isValidInput = (num) => {
// check that this is a number, in range, and an integer.
return !!(
typeof num === 'number'
&& num >= lowerBound
&& num <= upperBound
&& num % 1 === 0 // integer check
);
};
const startGame = () => {
console.log("Starting new game...");
// clear prompt and button click.
$("#prompt").hide();
$("#submit").off("click");
$("#test").off("click");
// generate random upper and lower bound.
// Lower: between -100 and 100;
lowerBound = Math.round(Math.random() * 100 - Math.random() * 100);
// Upper: between (lower + 10) and (lower + 1010)
upperBound = Math.round(Math.random() * 1000) + 10 + lowerBound;
// update tooltip on page.
$("#upper").html(upperBound);
$("#lower").html(lowerBound);
// set input max and min, and clear input value from last game
$("#inp").attr({
max: upperBound,
min: lowerBound,
}).val(null);
// generate a new random number to guess.
number = Math.round(Math.random() * (upperBound - lowerBound) + lowerBound);
// show the game area, bind the submit button.
$("#submit").on("click", guess);
$("#main").show();
// change "Play" to "New Game"
$("#play").html("New Game");
// enable test button and show test area
$("#test").on("click", () => {
if (showAlert) {
showAlert = false;
alert("For the tests, see the browser's console for output!");
}
testValidateInput(isValidInput, upperBound, lowerBound);
})
$("#mainTest").show();
console.log("New game started.");
};
const win = () => {
// disable submit button, and tell user they won.
console.log('User won!');
$("#submit").off("click");
$("#prompt")
.html(`Correct! You win! The number was indeed ${number}.`)
.show();
};
const higher = (num) => {
// tell user they should go higher.
console.debug('Higher!');
$("#prompt").html(`Nope, not ${num}. Higher!`).show();
};
const lower = (num) => {
// tell user they should go lower.
console.debug('Lower!');
$("#prompt").html(`Nope, not ${num}. Lower!`).show();
};
const guess = () => {
console.log("Checking user guess...");
let isValid = false;
let userGuess;
// get user input and validate it.
try {
userGuess = parseInt($("#inp").val());
console.log("User inputted", userGuess);
isValid = isValidInput(userGuess);
} catch(e) {
console.warn(e);
isValid = false;
}
if (!isValid) {
// tell the user to input correctly
$("#prompt").html(`Please enter an integer in the range, not "${userGuess}".`);
console.error('Invalid Input', userGuess);
return;
}
if (userGuess < number) {
// go higher!
higher(userGuess);
} else if (userGuess > number) {
// go lower!
lower(userGuess);
} else {
if (userGuess === number) {
// you won!
win();
} else {
// something weird happened.
console.error("Impossible case hit:", userGuess, number);
}
}
};
// activate play button.
$("#play").on("click", startGame);
});
</script>
</body>
</html>