-
Notifications
You must be signed in to change notification settings - Fork 0
/
highLowGame.js
41 lines (37 loc) · 1.21 KB
/
highLowGame.js
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
$(document).ready(function(){
$("button#begin").click(function() {
var computer = Math.floor(Math.random()*100+1);
console.log(computer);
var numberOfAttempts = 0;
for(i = 1; i <= 100; i++) {
$('body').append('<div class="numbers" id="div'+ i +'" >' + i + '</div>');
$("div#div" + i).click(function(){
var currentAttempt = parseInt($(this).text());
$(this).css("visibility", "hidden");
$(".answer").empty();
numberOfAttempts++;
// Check users answer
if (computer === currentAttempt) {
$(".answer").append("You got it!");
$("div").off("click");
$("button#begin").click(function(){
location.reload();
});
}
else if (computer < currentAttempt) {
$(".answer").append("Too High!");
}
else if (computer > currentAttempt) {
$(".answer").append("Too Low!");
}
if (numberOfAttempts === 7) {
alert("You've used up all your chances! The number was: " + computer);
$("div").off("click");
$("button#begin").click(function(){
location.reload();
});
}
});
}
}); // end button#begin
});