-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (43 loc) · 1.56 KB
/
script.js
File metadata and controls
52 lines (43 loc) · 1.56 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
const checkboxes = document.querySelectorAll('.card__checkbox');
let firstCard, secondCard;
function handleCheck() {
if (this !== firstCard) {
secondCard = this;
if (firstCard && secondCard) {
if (firstCard.nextElementSibling.querySelector('img').src === secondCard.nextElementSibling.querySelector('img').src) {
firstCard.checked = true;
secondCard.checked = true;
firstCard = null;
secondCard = null;
} else {
setTimeout(() => {
firstCard.checked = false;
secondCard.checked = false;
firstCard = null;
secondCard = null;
}, 1000);
}
} else {
firstCard = this;
}
}
}
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
const cards = document.querySelectorAll('.card__label');
const imgUrls = [ './images/african-village.jpeg', './images/plane.jpeg', './images/university.jpeg', './images/water.jpeg'];
function shuffle(array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
shuffle(imgUrls);
for (let i = 0; i < cards.length; i++) {
cards[i].querySelector('img').src = imgUrls[i % imgUrls.length];
}