-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment5.js
More file actions
74 lines (66 loc) · 1.98 KB
/
assignment5.js
File metadata and controls
74 lines (66 loc) · 1.98 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//registers a first card and a second card
let cardFlipped = false;
//prevents more than 2 cards from being opened at a time
let freezeCards = false;
let numberMatches = 0;
cards = document.querySelectorAll('.memoryCard');
//puts cards in random order on page load
(function shuffleCards() {
cards.forEach (card => {
let position = Math.floor(Math.random() * 12);
card.style.order = position;
});
})();
//choosing cards
var gamePlay = cards.forEach(item => {
item.addEventListener('click', () => {
if (freezeCards === true){
return;
}
item.classList.add('flip');
if (cardFlipped === false) {
cardFlipped = true;
firstCard = event.target.parentNode;
cardOne = firstCard.id;
return(cardOne);
}else{
if (event.target.parentNode !== firstCard){
cardFlipped = false;
freezeCards = true;
secondCard = event.target.parentNode;
setTimeout(isMatch, 1000);
cardTwo = secondCard.id;
return(cardTwo);
}
}
})
});
//check to see if cards match
function isMatch() {
//if it's a match
if (cardOne === cardTwo) {
firstCard.removeEventListener('click', gamePlay);
secondCard.removeEventListener('click', gamePlay);
freezeCards = false;
numberMatches += 1;
console.log(numberMatches);
if (numberMatches === 6){
showModal()
}
//if it's not a match
}else {
firstCard.classList.remove('flip');
secondCard.classList.remove('flip');
freezeCards = false;
}
};
//popup window on game win
let modal = document.querySelector(".modal");
function showModal() {
modal.classList.remove('modal');
modal.classList.toggle("show-modal");
}
//start new game
function newGame(){
location.reload();
};