-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
59 lines (47 loc) · 1.09 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
var openedCards = [];
var matches = 0;
var cards = document.querySelectorAll(".cards");
function swapImages()
{
openedCards.push(this);
if(openedCards.length === 2)
{
if(openedCards[0].src == this.src)
{
matches +=1;
document.getElementById('matches').innerHTML = matches;
openedCards = [];
}
else
{
unmatched();
}
}
}
function unmatched()
{
setTimeout(function(){
openedCards.forEach(element => {element.src = "images/green.png"});
openedCards = [];
}, 500);
}
function displayImages()
{
let image = "";
switch(this.id)
{
case 'elephant':
image = "images/elephant.jpg";
break;
case 'tiger':
image = "images/tiger.png" ;
break;
case 'rabbit':
image = "images/rabbit.png" ;
break;
}
this.src = image;
}
cards.forEach(element => {element.addEventListener('click', displayImages);
element.addEventListener('click', swapImages)
});