-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber-guessing.html
More file actions
146 lines (127 loc) · 4.4 KB
/
number-guessing.html
File metadata and controls
146 lines (127 loc) · 4.4 KB
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
<html>
<head>
<title>Guess the Number!</title>
</head>
<body>
<label id="games-won"></label>
<br />
<label>Pick a Number from 1-100:</label>
<br />
<label id="response"></label>
<br />
<br />
<input id="user-input" type="text" />
<input id="submit-button" type="button" value="Guess" />
<br />
<br />
<label id="landmine"></label>
<br />
<br />
<label>Start a new Game!</label>
<br />
<input id="new-game" type="button" value="Start Again!" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// SIMPLE VANILLA JS VERSION
// *************************
// var secretNumber = parseInt(Math.random() * 100, 10) + 1;
// var userNumber = function() {
// var number = prompt("Please, choose a number between 1 and 100");
// if (number > 100 || number < 1) {
// alert("The number has to be between 1 and 100");
// userNumber();
// } else if (number < secretNumber) {
// alert("Pick a higher one!");
// userNumber();
// } else if (number > secretNumber) {
// alert("Pick a lower one!");
// userNumber();
// } else if (isNaN(number) === true) {
// alert("That's not a number!");
// userNumber();
// } else {
// alert("You guessed it right! You win!");
// }
// }
// secretNumber;
// userNumber();
// JQUERY FLAVORED VERSION
// ***********************
var counter = 0;
var landMine = 56;
var secretNumber = parseInt(Math.random() * 100, 10) + 1;
$('#response').text('Welcome to the Number Guessing Game!');
$('#landmine').text("Be careful, there's is a landmine around!");
var numCompare = function() {
$('#submit-button').on('click', function (e) {
var input = $('#user-input').val();
alert('You typed in: ' + input);
if (input == secretNumber) {
var gamesWon = function () {
counter = counter + 1;
return(counter);
};
$("#games-won").text("Total wins: " + gamesWon());
$('#response').text("Congrats! You won!");
if (input == landMine) {
var gamesLost = function () {
counter = counter - 1;
return(counter);
};
gamesLost();
$("#response").text("You stepped on the landmine! You lost!");
} else if (input < landMine) {
$('#landmine').text("You are below the landmine number. Stay away or die!");
return numCompare;
} else if (input > landMine) {
$('#landmine').text("You are above the landmine number. Stay away or die!");
return numCompare;
};
} else if (input > secretNumber) {
$('#response').text("You guessed to high! Try again!");
if (input == landMine) {
var gamesLost = function () {
counter = counter - 1;
return(counter);
};
gamesLost();
$("#response").text("You stepped on the landmine! You lost!")
} else if (input < landMine) {
$('#landmine').text("You are below the landmine number. Stay away or die!");
return numCompare;
} else if (input > landMine) {
$('#landmine').text("You are above the landmine number. Stay away or die!");
return numCompare;
};
} else if (input < secretNumber) {
$('#response').text("You guessed to low! Try again!");
if (input == landMine) {
var gamesLost = function () {
counter = counter - 1;
return(counter);
};
gamesLost();
$("#response").text("You stepped on the landmine! You lost!")
} else if (input < landMine) {
$('#landmine').text("You are below the landmine number. Stay away or die!");
return numCompare;
} else if (input > landMine) {
$('#landmine').text("You are above the landmine number. Stay away or die!");
return numCompare;
};
} else {
$('#response').text("That's not a number! Try again!");
return numCompare;
}
});
};
numCompare();
$("#new-game").on('click', function (e) {
secretNumber = parseInt(Math.random() * 100, 10) + 1;
$("#user-input").val("");
$("#response").text('A New Game has started!!');
numCompare();
});
</script>
</body>
</html>