-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
124 lines (124 loc) Β· 3.5 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
var memoji = ['π','π','π¦','π¦','π','π','π΅','π΅','πΈ','πΈ','π°','π°'];
var box = document.querySelector('.grid-box');
var elemList = Array.from(document.querySelectorAll("div.front"));
var cards = Array.from(document.querySelectorAll(".card"));
var timer = document.querySelector('.timer');
var modal = document.querySelector('.modal');
var gameResult = document.querySelector('.game-result');
var button = document.querySelector('button');
var openedCards;
var numberOfopened;
var time;
var timerId;
var isTimerStarted;
var front;
function shuffle(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var t = array[i];
array[i] = array[j];
array[j] = t;
}
}
function matchCards() {
front = openedCards.map(getFront);
if (front[0].innerHTML == front[1].innerHTML) {
front.forEach(function(fronti) {
fronti.classList.add('matched');
})
numberOfopened += 2;
openedCards = [];
if (numberOfopened == 12) {
popUp('Win', 'Play again');
}
} else {
front.forEach(toggleNotMatched);
}
}
function getFront(card) {
return card.querySelector('.front');
}
function toggleNotMatched (item) {
item.classList.toggle('not-matched');
}
function rotate(card) {
card.classList.toggle('non-clickable');
card.classList.toggle('card-rotated');
}
function openCard(card) {
if (!isTimerStarted) {
isTimerStarted = true;
timerId = setInterval(timerHandler, 1000);
}
if (openedCards.length == 2) {
front = openedCards.map(getFront);
front.forEach(toggleNotMatched);
openedCards.forEach(rotate);
openedCards = [];
}
if (openedCards.length == 0) {
rotate(card);
openedCards.push(card);
} else {
rotate(card);
openedCards.push(card);
matchCards();
}
}
function timerHandler() {
if (--time > 9) {
timer.innerHTML = '00:' + time;
} else {
timer.innerHTML = '00:0' + time;
}
if (time == 0) {
popUp('Lose', 'Try again');
}
}
function popUp(result, buttonMessage) {
clearInterval(timerId);
modal.classList.add('visible');
gameResult.innerHTML = '';
fillResult(result);
button.innerHTML = buttonMessage;
}
function fillResult(result) {
for (var i = 0; i < result.length; i++) {
var newLetter = document.createElement('span');
newLetter.innerText = result[i];
gameResult.appendChild(newLetter);
}
}
function restart() {
cards.forEach(function(elem, index) {
elem.classList.remove("card-rotated", "non-clickable");
})
cards.map(getFront).forEach(function(elem, index) {
elem.classList.remove("matched", "not-matched");
})
shuffle(memoji);
setTimeout(function() {
elemList.forEach(function(elem, index) {
elem.innerHTML = memoji[index];
})
}, 250);
timer.innerHTML = '01:00';
time = 60;
isTimerStarted = false;
openedCards = [];
numberOfopened = 0;
}
box.addEventListener('click', function(event) {
if (event.target.parentNode.classList.contains('card')) {
openCard(event.target.parentNode);
}
}, true)
button.addEventListener('mousedown', function(event) {
event.target.classList.add('clicked');
});
button.addEventListener('mouseup', function(event) {
event.target.classList.remove('clicked');
modal.classList.remove('visible');
restart();
});
window.onload = restart();