-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (120 loc) · 2.87 KB
/
index.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
const cards = [
{
name: 'apple',
src: './images/apple.jpg',
alt: 'apple',
isFlipped: false,
},
{
name: 'bread',
src: './images/bread.jpg',
alt: 'bread',
isFlipped: false,
},
{
name: 'broccoli',
src: './images/broccoli.jpg',
alt: 'broccoli',
isFlipped: false,
},
{
name: 'coffee',
src: './images/coffee.jpg',
alt: 'coffee',
isFlipped: false,
},
{
name: 'donut',
src: './images/donut.jpg',
alt: 'donut',
isFlipped: false,
},
{
name: 'meat',
src: './images/meat.jpg',
alt: 'meat',
isFlipped: false,
},
{
name: 'milk',
src: './images/milk.jpg',
alt: 'milk',
isFlipped: false,
},
{
name: 'pizza',
src: './images/pizza.jpg',
alt: 'pizza',
isFlipped: false,
},
{
name: 'squit',
src: './images/squit.jpg',
alt: 'squit',
isFlipped: false,
},
{
name: 'peanut',
src: './images/peanut.jpg',
alt: 'peanut',
isFlipped: false,
},
]
cards.push(...cards)
cards.sort(() => 0.5 - Math.random())
const cardsBoard = document.querySelector('.cards-board')
let score = 0
const chosenCards = []
const chosenCardsIds = []
function createBoard() {
let i = 0
cards.forEach((card) => {
const img = document.createElement('img')
img.setAttribute('src', './images/cards-bg.jpg')
img.setAttribute('data-id', i)
i++
img.addEventListener('click', flipCard)
cardsBoard.appendChild(img)
})
}
createBoard()
// flips the cards and put the flipped Cards into a new array
function flipCard() {
const cardId = this.getAttribute('data-id')
this.setAttribute('src', cards[cardId].src)
chosenCards.push(cards[cardId].name)
chosenCardsIds.push(cardId)
if (chosenCards.length === 2) {
setTimeout(checkMatch, 500)
}
}
function checkMatch() {
const scoreElement = document.getElementById('score')
const img = document.querySelectorAll('img')
if (chosenCardsIds[0] === chosenCardsIds[1]) {
alert('You clicked the same card, please choose two different')
img[chosenCardsIds[0]].setAttribute('src', './images/cards-bg.jpg')
img[chosenCardsIds[1]].setAttribute('src', './images/cards-bg.jpg')
chosenCards.splice(0, 2)
chosenCardsIds.splice(0, 2)
} else if (chosenCards[0] == chosenCards[1]) {
score++
scoreElement.innerText = score
// cannot be added to the chosenCards Array and cursor we be set to default
chosenCardsIds.forEach((id) => {
img[id].removeEventListener('click', flipCard)
img[id].setAttribute('src', './images/clear.jpeg')
img[id].style.cursor = 'default'
img[id].style.border = 'none'
})
} else {
// flip back the card if is not a match
chosenCardsIds.forEach((id) => {
setTimeout(() => {
img[id].setAttribute('src', './images/cards-bg.jpg')
}, 1500)
})
}
chosenCards.splice(0, 2)
chosenCardsIds.splice(0, 2)
}