-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
283 lines (258 loc) · 9.08 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
<!DOCTYPE html>
<html>
<head>
<title>Hot or Cold</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<style>
#buttons div {
padding: 2px;
}
#buttons .btn-large {
width: 90px;
height: 50px;
}
#newGameButton {
margin-top: 10px;
}
#messages {
width: 350px;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class='container text-center'>
<h2 class='row'>Hot or Cold</h2>
<div class='text-center row'>
<div id='messages' class='alert alert-info'>
Guess a number
</div>
</div>
<div id='inputs' class='input-prepend input-append row'>
<input type='text' id='userInput' class='input-small text-center' />
<button type='button' id='guessButton' class='btn'>Guess</button>
<button type='button' id='guessCount' class='btn'>0</button>
</div>
<div id='buttons' class='row'>
<div class=''>
<button id='button_1' class='btn btn-large'>1</button>
<button id='button_2' class='btn btn-large'>2</button>
<button id='button_3' class='btn btn-large'>3</button>
</div>
<div class=''>
<button id='button_4' class='btn btn-large'>4</button>
<button id='button_5' class='btn btn-large'>5</button>
<button id='button_6' class='btn btn-large'>6</button>
</div>
<div class=''>
<button id='button_7' class='btn btn-large'>7</button>
<button id='button_8' class='btn btn-large'>8</button>
<button id='button_9' class='btn btn-large'>9</button>
</div>
<div class=''>
<button type='button' id='resetButton' class='btn btn-large'>C</button>
<button id='button_0' class='btn btn-large'>0</button>
<button type='button' id='backButton' class='btn btn-large'>
Back
</button>
</div>
</div>
</div>
<div class='text-center row'>
<button type='button' id='newGameButton' class='btn btn-large'>New Game</button>
</div>
<script type='text/javascript'>
var HC = function($) {
var h = {};
// This should create a privately scoped number
var randomNumber;
h.init = function() {
h.data = {
min: 1,
max: 100
};
// Init references
h.$userInput = $("#userInput");
h.$guessButton = $("#guessButton");
h.$guessCount = $("#guessCount");
h.$resetButton = $("#resetButton");
h.$backButton = $("#backButton");
h.$buttons = $("#buttons .btn");
h.$messages = $("#messages");
h.$newGameButton = $("#newGameButton");
// Bind Events
h.$buttons.on('click', h.setUserInput);
h.$resetButton.on('click', h.resetUserInput);
h.$backButton.on('click', {isBack: true}, h.resetUserInput);
$(document).on('keydown', h.triggerButton);
h.$guessButton.on('click', h.guessNumber);
h.$userInput
.on('keydown', h.isEnterKey)
.on('keyup', function(e) {
if (e.keyCode != 13) {
h.validateInput(h.$userInput.val());
} else {
e.preventDefault();
return false;
}
});
h.$newGameButton.on('click', h.resetGame);
h.generateRandomNumber();
}
h.setUserInput = function(e) {
e.preventDefault();
var currentValue = h.$userInput.val();
var newValue = currentValue + $(this).text();
if (h.validateInput(newValue)) {
h.$userInput.val(newValue);
}
return false;
} // setUserInput
h.validateInput = function(val) {
// Remove all alert classes
h.$messages.removeClass('alert-info alert-error alert-success')
// If within thresholds, it's valid
if (val >= h.data.min && val <= h.data.max) {
h.$messages
.text("Press <enter> or click the Guess Button")
.addClass('alert-info');
return true;
} else if (val == '') { // If blank, show default message
h.$messages
.text("Guess a number")
.addClass('alert-info');
return true;
} else { // Else it's not valid, alert the user
h.$messages
.text('Please enter a valid number between ' + h.data.min + ' and ' + h.data.max)
.addClass('alert-error');
// Remove non numeric
val = val.replace(/[^\d]/g, '');
// Cut it down to two characters
if (val.length > 2) {
val = val.substring(0, val.length - 1);
}
h.$userInput.val(val);
return false;
}
} // validateInput
h.resetUserInput = function(e) {
e.preventDefault();
if (e.data == undefined) {
h.$userInput.val('');
h.validateInput(h.$userInput.val());
} else if(e.data.isBack) {
// Indicates the back button was pressed, only remove last number
var currentValue = h.$userInput.val();
var newValue = currentValue.substring(0, currentValue.length - 1);
h.$userInput.val(newValue);
h.validateInput(h.$userInput.val());
}
return false;
} // resetUserInput
h.triggerButton = function(e) {
// Get the value of the key pressed
var buttonNum = String.fromCharCode(e.keyCode);
// If it is numeric, trigger the associated button
if (buttonNum >= 0 && buttonNum <= 9 && Number(e.keyCode) !== 13) {
e.preventDefault();
$("#button_" + buttonNum).trigger('click');
return false;
} else if (buttonNum == 'c' || buttonNum == 'C') {
// If the key pressed was a C, trigger the clear button
e.preventDefault();
h.$resetButton.trigger('click');
return false;
} else if (e.keyCode == 8) {
// If the backspace button was pressed, trigger the back button
e.preventDefault();
h.$backButton.trigger('click');
return false;
} else if (e.keyCode == 13) {
// If the enter key is pressed, trigger guess button
e.preventDefault();
h.$guessButton.trigger('click');
return false;
}
} // triggerButton
h.generateRandomNumber = function() {
randomNumber = Math.floor((Math.random() * (h.data.max - h.data.min + 1)) + h.data.min);
} // generateRandomNumber
h.isEnterKey = function(e) {
if (e.keyCode == 13) {
e.preventDefault();
h.guessNumber();
return false;
}
} // isEnterKey
h.guessNumber = function() {
var guess = h.$userInput.val();
if (guess == '') {
h.$messages
.removeClass('alert-info alert-error alert-success')
.text('Please enter a valid number between ' + h.data.min + ' and ' + h.data.max)
.addClass('alert-error');
} else {
// Increment the guess counter
h.$guessCount.text(Number(h.$guessCount.text()) + 1);
if (guess != randomNumber) {
// Didn't guess correctly, let's get the difference and display a message
var diff = randomNumber - guess;
h.displayErrorMessage(diff);
} else {
// Guessed correctly!
h.$messages
.removeClass('alert-info alert-error alert-success')
.text('You guessed the correct number: ' + randomNumber)
.addClass('alert-success');
}
h.$userInput.val('');
}
} // guessNumber
h.displayErrorMessage = function(diff) {
var message = '';
var className = '';
// Want to get the absolute value
diff = Math.abs(diff);
if (diff >= 50) {
message = 'Freezing';
} else if (diff >= 40) {
message = 'Cold';
} else if (diff >= 30) {
message = 'Chilly';
} else if (diff >= 20) {
message = 'Lukewarm';
} else if (diff >= 15) {
message = 'Warm';
} else if (diff >= 10) {
message = 'Hot';
} else if (diff > 1) {
message = 'On Fire';
} else {
message = 'Scortching';
}
// Update the message
h.$messages
.removeClass('alert-info alert-error alert-success')
.text(message)
.addClass('alert-info');
} // displayErrorMessage
h.resetGame = function() {
h.$messages
.removeClass('alert-info alert-error alert-success')
.text("Guess a number")
.addClass('alert-info');
h.$guessCount.text(0);
h.$userInput.val('');
h.generateRandomNumber();
} // resetGame
return h;
}($);
$(document).ready(function() {
HC.init();
});
</script>
</body>
</html>