-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (109 loc) · 3.46 KB
/
app.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
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
"use strict";
const resetBtn = document.getElementById("resetBtn");
const inputNumber = document.getElementById("numberBox");
const checkBtn = document.getElementById("checkBtn");
const gameLife = document.getElementById("gameLife");
const winStreak = document.getElementById("winStreak");
const measureNumber = document.getElementById("numberMeasurement");
const displayScore = document.getElementById("displayNumberBox");
const bodyColor = document.getElementById("bodyColor");
const gameForm = document.getElementById("gameForm");
const min = 1;
const max = 20;
let randomNumber = Math.trunc(Math.random() * max) + min;
let gameChance = parseInt(gameLife.textContent);
let winCount = parseInt(winStreak.textContent);
const resetColor = () => {
bodyColor.style.backgroundColor = "var(--primaryBackgroundColor)";
};
// modal pop-up
$("#closebutton").click(function () {
$(".modal-backdrop").remove();
});
document.querySelector("#closebutton").addEventListener("click", function () {
resetGameData();
document.querySelector(".modal").hide();
});
document.querySelector("#closeBtn").addEventListener("click", function () {
resetGameData();
document.querySelector(".modal").hide();
});
const resetGameData = function () {
// go back to the initial state
measureNumber.textContent = "💭 Start Guessing...";
gameLife.textContent = `20`;
winStreak.textContent = `0`;
displayScore.textContent = "?";
gameChance = 20;
winCount = 0;
randomNumber = Math.trunc(Math.random() * max) + min;
gameForm.reset();
resetColor();
};
const decreaseGameChance = function () {
gameChance--;
gameLife.textContent = gameChance;
if (gameChance == 0) {
measureNumber.textContent = `❌ You lost the game!`;
bodyColor.style.backgroundColor = "#ff0000";
$(document).ready(function () {
setTimeout(function () {
$("#myModal").modal("show");
}, 100);
});
}
};
const resetInfo = () => {
displayScore.textContent = "?";
measureNumber.textContent = "💭 Start Guessing...";
bodyColor.style.backgroundColor = "#222";
randomNumber = Math.trunc(Math.random() * max) + min;
gameForm.reset();
};
function startGame() {
checkBtn.addEventListener("click", function () {
if (inputNumber.value == "") {
measureNumber.textContent = `⚠️ Enter a correct number!`;
bodyColor.style.backgroundColor = "#ff0000";
} else if (inputNumber.value < 1 || inputNumber.value > 20) {
measureNumber.innerHTML = `<h5>⛔ Out of Range!</h5>`;
decreaseGameChance();
} else {
if (inputNumber.value == randomNumber) {
bodyColor.style.backgroundColor = "#60b347";
displayScore.textContent = randomNumber;
measureNumber.textContent = "✨ Correct number!";
winCount++;
winStreak.textContent = winCount;
if (winCount == 5) {
// display modal to show that you are the winner here bs-modal
measureNumber.textContent = "🏆 You won the game!";
$(document).ready(function () {
setTimeout(function () {
$("#myModalTwo").modal("show");
}, 100);
});
} else {
setTimeout(resetInfo, 1700);
}
} else if (inputNumber.value > randomNumber) {
measureNumber.textContent = "📈 Too high!";
decreaseGameChance();
} else {
measureNumber.textContent = "📉 Too low!";
decreaseGameChance();
}
}
});
}
// reset the game data
resetBtn.addEventListener("click", function () {
const removeGameData = window.confirm(
"Are you sure you want to reset the game data?"
);
if (removeGameData) {
resetGameData();
} else {
startGame();
}
});