-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
74 lines (67 loc) · 1.88 KB
/
code.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
let clickedCard = null;
let preventClick = false;
let combosFound = 0;
const colors = [
'pink',
'yellow',
'red',
'cyan',
'blue',
'teal',
'orange',
'green',
]
const cards = [...document.querySelectorAll('.card')];
for(let color of colors){
const cardAIndex = parseInt(Math.random() * cards.length);
const cardA = cards[cardAIndex];
cards.splice(cardAIndex, 1);
cardA.className += `${color}`;
cardA.setAttribute('data-color', color);
const cardBIndex = parseInt(Math.random() * cards.length);
const cardB = cards[cardBIndex];
cards.splice(cardBIndex, 1);
cardB.className += `${color}`;
cardB.setAttribute('data-color',color);
}
function onCardClicked(e) {
const target = e.currentTarget;
if (
preventClick ||
target === clickedCard ||
target.className.includes('done')) {
return;
}
target.className = target.className
.replace('color-hidden', '')
.trim();
target.className += ' done';
// console.log(target.getAttribute('data-color'));
if(!clickedCard){
clickedCard = target;
} else if (clickedCard){
if(
clickedCard.getAttribute('data-color') !==
target.getAttribute('data-color')
) {
preventClick = true;
setTimeout(() => {
console.log('not a match')
clickedCard.className =
clickedCard.className.replace(' done', '').trim() +
'color-hidden';
target.className =
target.className.replace(' done','').trim() +
'color-hidden';
clickedCard = null;
preventClick = false;
}, 500);
} else {
combosFound++;
clickedCard = null;
if (combosFound === 3){
alert("YOU WIN!");
}
}
}
}