|
@@ -0,0 +1,42 @@
|
|
1
|
+var randomNum = Math.floor(Math.random() * 100) + 1;
|
|
2
|
+var guessCount = 0;
|
|
3
|
+
|
|
4
|
+function startGame() {
|
|
5
|
+ var gameDiv = document.getElementById("gameId");
|
|
6
|
+ gameDiv.style.visibility = "visible";
|
|
7
|
+}
|
|
8
|
+
|
|
9
|
+function gameFunc() {
|
|
10
|
+ guessCount ++;
|
|
11
|
+
|
|
12
|
+ var startBtn = document.getElementById("start");
|
|
13
|
+ var input = document.getElementById("guess");
|
|
14
|
+ var result = document.getElementById("result");
|
|
15
|
+ var restart = document.getElementById("restart");
|
|
16
|
+ var playerGuess = input.value;
|
|
17
|
+ var guessBtn = document.getElementById("guessBtn");
|
|
18
|
+
|
|
19
|
+ if (isNaN(playerGuess) ||
|
|
20
|
+ !Number.isInteger(parseInt(playerGuess)) ||
|
|
21
|
+ parseInt(playerGuess) <= 0 ||
|
|
22
|
+ parseInt(playerGuess) > 100) {
|
|
23
|
+ alert("Please enter an integer between 1 and 100");
|
|
24
|
+ input.value = "";
|
|
25
|
+ } else if (parseInt(playerGuess) == randomNum) {
|
|
26
|
+ result.innerHTML = "You are right!!! " + randomNum + " is a correct guess. You made it right in " + guessCount + " guesses";
|
|
27
|
+ startBtn.disabled = "true";
|
|
28
|
+ guessBtn.disabled = "true";
|
|
29
|
+ input.disabled = "true";
|
|
30
|
+ restart.style.visibility = "visible";
|
|
31
|
+ } else if (parseInt(playerGuess) > randomNum) {
|
|
32
|
+ result.innerHTML = "It's lower";
|
|
33
|
+ input.value = "";
|
|
34
|
+ } else if (parseInt(playerGuess) < randomNum) {
|
|
35
|
+ result.innerHTML = "It's higher";
|
|
36
|
+ input.value = "";
|
|
37
|
+ }
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+function restart() {
|
|
41
|
+ window.location.reload();
|
|
42
|
+}
|