-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhack_a_mole.js
More file actions
54 lines (43 loc) · 1.23 KB
/
Copy pathwhack_a_mole.js
File metadata and controls
54 lines (43 loc) · 1.23 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
const startTheGame = document.getElementById('start-button');
const moles = document.querySelectorAll('.mole');
let scores = document.querySelector('.scores');
let moleId = null;
let timerForStart = null;
startTheGame.addEventListener('click', start);
function getRandomMole() {
let randomMoleIndex = Math.floor(Math.random() * moles.length);
if (randomMoleIndex !== moleId) {
moleId = randomMoleIndex;
return randomMoleIndex;
} else {
return getRandomMole();
}
}
function getRandomDelay(min, max) {
return Math.round(min + Math.random() * (max - min));
}
function plusOne() {
scores.textContent = +scores.textContent + 1;
}
function moleUp(mole) {
moles[mole].classList.add('mole_over-ground');
}
function moleDown() {
moles.forEach(mole => mole.classList.remove('mole_over-ground'));
}
function getMovedTheMole() {
const mole = getRandomMole();
const delayForGetMoved = getRandomDelay(400, 1000);
moleDown();
moleUp(mole);
timerForStart = setTimeout(getMovedTheMole, delayForGetMoved);
}
moles.forEach(mole => mole.addEventListener('click', plusOne));
function start() {
scores.textContent = 0;
getMovedTheMole();
setTimeout(() => {
moleDown();
clearTimeout(timerForStart)
}, 10e3);
}