-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (148 loc) · 5.97 KB
/
script.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
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
window.addEventListener("beforeunload", (event) => {
if (parseInt(scoreDisplayBox.textContent) > 0) {
// Display a confirmation message
event.returnValue = "Leaving/reloading this page will reset your score. Are you sure you want to leave?";
}
});
let randomNumArr = [];
/**
* Generates div elements with bubble class having random numbers and ultimately appending it to the "pbtm" element.
*
* @return {undefined} No return value
*/
const panelBottom = document.getElementById("pbtm");
function generateBubble(bubbleCount, bubbleDimenions) {
const fragment = document.createDocumentFragment();
for (let i = 1; i <= bubbleCount; i++) {
const randomNum = Math.ceil(Math.random() * 99);
randomNumArr.push(randomNum);
const bubble = document.createElement('div');
bubble.classList.add('bubble');
bubble.style.height = bubbleDimenions;
bubble.style.width = bubbleDimenions;
bubble.textContent = randomNum;
fragment.appendChild(bubble);
}
panelBottom.appendChild(fragment);
}
/**
* Starts a timer that counts down from a specified value as stored in variable `timerCount`
*
* @return {undefined} This function does not return a value.
*/
let intervalId, currentTimerCount;
function startTimer(timerCount) {
intervalId = setInterval(() => {
if (timerCount === 0 || timerCount < 0) {
// alert("Time Up!");
document.querySelector('#loserMessageModalContainer').style.display = 'flex'
panelBottom.innerHTML = '';
clearInterval(intervalId);
}
else if (randomNumArr.length === 0) {
document.querySelector('#winnerMessageModalContainer').style.display = 'flex'
clearInterval(intervalId);
}
else {
timerCount--;
document.querySelector("#timerLiveCount").textContent = `${timerCount}s`;
}
currentTimerCount = timerCount;
}, 1000)
}
let randomNumber;
/**
* Picks a random number from the array 'randomNumArr' and displays it in the element with the id 'numberToHitDisplayBox'.
*
* @return {number} The randomly picked number.
*/
function generateRandomNumberToHit() {
randomNumber = randomNumArr[Math.floor(Math.random() * randomNumArr.length)];
document.querySelector("#numberToHitDisplayBox").textContent = randomNumber;
return randomNumber;
}
/**
* Adds event listeners to bubbles.
*
* This function selects all elements with the class "bubble" and adds a click event listener to each one.
* When a bubble is clicked, the function checks if its text content matches the global variable "randomNumber".
* If there is a match, the bubble is removed from the DOM, the global array "randomNumArr" is updated to remove the matched number,
* a new random number for hitting is picked, and the score displayed in the element with the id "scoreDisplayBox" is incremented by 1.
*
* @return {undefined} This function does not return a value.
*/
const scoreDisplayBox = document.querySelector("#scoreDisplayBox");
function addEventListenerToBubbles() {
document.querySelectorAll('.bubble').forEach(bubble => {
bubble.addEventListener('click', () => {
if (bubble.textContent == randomNumber) {
randomNumArr.splice(randomNumArr.indexOf(parseInt(bubble.textContent)), 1);
bubble.remove();
generateRandomNumberToHit();
scoreDisplayBox.textContent = parseInt(scoreDisplayBox.textContent) + 10;
}
else {
alert('Wrong Bubble!');
if (parseInt(scoreDisplayBox.textContent) !== 0) {
scoreDisplayBox.textContent = parseInt(scoreDisplayBox.textContent) - 5;
}
}
})
})
}
addEventListenerToBubbles();
const difficultyChoiceDropdown = document.querySelector('#difficultyChoiceBox');
difficultyChoiceDropdown.addEventListener('change', () => {
document.querySelector('#pbtm').innerHTML = '';
randomNumArr = [];
scoreDisplayBox.textContent = '0';
clearInterval(intervalId);
const selectedDifficultyOption = difficultyChoiceDropdown.options[difficultyChoiceDropdown.selectedIndex];
const selectedDifficulty = selectedDifficultyOption.value;
switch (selectedDifficulty) {
case 'easy':
generateBubble(32, "100px");
generateRandomNumberToHit();
addEventListenerToBubbles();
startTimer(150);
break;
case 'medium':
generateBubble(36, "60x");
generateRandomNumberToHit();
addEventListenerToBubbles();
startTimer(330);
break;
case 'hard':
generateBubble(100, "45px");
generateRandomNumberToHit();
addEventListenerToBubbles();
startTimer(630);
break;
default:
alert('Seems like an unknown error occured. Please try again.');
}
})
// JavaScript for Various Modals
const startGameModalButton = document.querySelector('#startGameModalButton');
startGameModalButton.addEventListener('click', () => {
document.querySelector('#startGameModalContainer').style.display = 'none';
if (window.innerWidth > 320) {
generateBubble(32, "100px");
}
else {
generateBubble(32, "50px");
}
addEventListenerToBubbles();
generateRandomNumberToHit();
startTimer(150);
})
const playAgainModalButton = document.querySelector('#playAgainModalButton');
playAgainModalButton.addEventListener('click', () => {
document.querySelector('#winnerMessageModalContainer').style.display = 'none';
document.querySelector('#startGameModalContainer').style.display = 'flex';
})
const tryAgainModalButton = document.querySelector('#tryAgainModalButton');
tryAgainModalButton.addEventListener('click', () => {
document.querySelector('#loserMessageModalContainer').style.display = 'none';
document.querySelector('#startGameModalContainer').style.display = 'flex';
})