-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
148 lines (136 loc) Β· 4.02 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
document.addEventListener('DOMContentLoaded', () => {
//card options
const cardArray = [
{
name: 'audi',
img: 'icons/audi-11.svg'
},
{
name: 'bentley',
img: 'icons/bentley.svg'
},
{
name: 'bmw',
img: 'icons/bmw.svg'
},
{
name: 'maserati',
img: 'icons/maserati.svg'
},
{
name: 'benz',
img: 'icons/mebenz.svg'
},
{
name: 'royal-enfield',
img: 'icons/re.svg'
},
{
name: 'tata',
img: 'icons/tata-motors.svg'
},
{
name: 'tesla-motors',
img: 'icons/tesla-motors-1.svg'
},
{
name: 'audi',
img: 'icons/audi-11.svg'
},
{
name: 'bentley',
img: 'icons/bentley.svg'
},
{
name: 'bmw',
img: 'icons/bmw.svg'
},
{
name: 'maserati',
img: 'icons/maserati.svg'
},
{
name: 'benz',
img: 'icons/mebenz.svg'
},
{
name: 'royal-enfield',
img: 'icons/re.svg'
},
{
name: 'tata',
img: 'icons/tata-motors.svg'
},
{
name: 'tesla-motors',
img: 'icons/tesla-motors-1.svg'
},
]
//cardArray.copyWithin(0,0);
console.log('cards',cardArray)
cardArray.sort(() => 0.5 - Math.random())
console.log('cards',cardArray)
const grid = document.querySelector('.grid')
const showResult = document.querySelector('#result')
var cardsChosen = []
var cardsChosenId = []
const matchedCards = [] || 0;
let totalTries = 0;
//create the playing π¬
//add img src data-id and append it to parent div
function createBoard() {
for (let i = 0; i < cardArray.length; i++) {
var card = document.createElement('img')
card.setAttribute('src', 'icons/blank.svg')
card.setAttribute('data-id', i)
card.addEventListener('click', flipCard)
grid.appendChild(card)
}
}
//flip card
//get the id , add it to array, change src from blank to img
function flipCard(){
let cardId = this.getAttribute('data-id');
cardsChosen.push(cardArray[cardId].name)
cardsChosenId.push(cardId);
this.setAttribute('src', cardArray[cardId].img)
if(cardsChosen.length == 2){
//check for macthes π
setTimeout(checkForMatch,600);
}
}
//check for match π¨βπ»
//its a match when two cards in the cardChoosen array are the same, here js reference to same objec so..
//its a victory if cardswon length == half the length
function checkForMatch(){
let cards = document.querySelectorAll('img')
const h3 = document.querySelector('h3');
const selectCardOneId = cardsChosenId[0];
const selectCardTwoId = cardsChosenId[1];
//deep equality
if(cardsChosen[0] === cardsChosen[1]){
alert('Its a match');
//remove the card (mark them cross)
cards[selectCardOneId].setAttribute('src','icons/cross.svg');
cards[selectCardTwoId].setAttribute('src','icons/cross.svg');
//cards[selectCardOneId].removeEventListener();
//cards[selectCardTwoId].removeEventListener();
//add to macthedCards
matchedCards.push(cardsChosen);
}else{
cards[selectCardOneId].setAttribute('src','icons/blank.svg');
cards[selectCardTwoId].setAttribute('src','icons/blank.svg');
totalTries++;
alert('oops! try again')
}
//reset the arrays
cardsChosen = []
cardsChosenId = []
showResult.textContent = matchedCards.length;
if(matchedCards.length === cardArray.length/2){
h3.textContent =`Yay! π`;
showResult.textContent = ` Congratulations! You macthed them all! Total tries : ${totalTries}`
}
}
createBoard();
})