-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
119 lines (102 loc) · 2.3 KB
/
app.js
File metadata and controls
119 lines (102 loc) · 2.3 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
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
document.addEventListener('DOMContentLoaded',() => {
//card Options
const cardArray = [
{
name: 'dino',
img: 'images/dino.png'
},
{
name: 'facebook',
img: 'images/facebook.png'
},
{
name: 'instagram',
img: 'images/instagram.png'
},
{
name: 'heart',
img: 'images/heart.png'
},
{
name: 's',
img: 'images/s.png'
},
{
name: 'telegram',
img: 'images/telegram.png'
},
{
name: 'dino',
img: 'images/dino.png'
},
{
name: 'facebook',
img: 'images/facebook.png'
},
{
name: 'instagram',
img: 'images/instagram.png'
},
{
name: 'heart',
img: 'images/heart.png'
},
{
name: 's',
img: 'images/s.png'
},
{
name: 'telegram',
img: 'images/telegram.png'
}
]
const grid = document.querySelector('.grid')
const resultDisplay = document.querySelector('#result')
var cardChosen = []
var cardChosenId = []
var cardsWon = []
// create your board
function createBoard() {
for (let i = 0; i < cardArray.length; i++) {
var card = document.createElement('img')
card.setAttribute('src','images/base.jpg')
card.setAttribute('data-id',i)
card.addEventListener('click',flipCard)
grid.appendChild(card)
}
}
cardArray.sort (() => 0.5 - Math.random())
//check for matches
function checkForMatch () {
var cards = document.querySelectorAll('img')
const optionOneID = cardChosenId[0]
const optionTwoID = cardChosenId[1]
if (cardChosen[0]=== cardChosen [1]) {
alert('you found a match')
cards[optionOneID].setAttribute('src','images/blank.png')
cards[optionTwoID].setAttribute('src','images/blank.png')
cardsWon.push(cardChosen)
} else {
cards[optionOneID].setAttribute('src','images/base.jpg')
cards[optionTwoID].setAttribute('src','images/base.jpg')
alert('Sorry Try again')
}
cardChosen = []
cardChosenId = []
resultDisplay.textContent = cardsWon.length
if (cardsWon.length === cardArray.length/2) {
resultDisplay.tectContent = 'Congrats!'
}
}
//flip your cardA
function flipCard(){
var cardId = this.getAttribute('data-id')
cardChosen.push(cardArray[cardId].name)
cardChosenId.push(cardId)
this.setAttribute('src',cardArray[cardId].img)
if (cardChosen.length === 2){
setTimeout(checkForMatch,500)
}
}
createBoard()
})