-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps.js
More file actions
121 lines (111 loc) · 3.13 KB
/
apps.js
File metadata and controls
121 lines (111 loc) · 3.13 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
120
121
const cardArray = [
{
name: 'fries',
img: 'images/fries.png'
},
{
name: 'cheeseburger',
img: 'images/cheeseburger.png'
},
{
name: 'hotdog',
img: 'images/hotdog.png'
},
{
name: 'milkshake',
img: 'images/milkshake.png'
},
{
name: 'pizza',
img: 'images/pizza.png'
},
{
name: 'ice-cream',
img: 'images/ice_cream.png'
},
{
name: 'fries',
img: 'images/fries.png'
},
{
name: 'cheeseburger',
img: 'images/cheeseburger.png'
},
{
name: 'hotdog',
img: 'images/hotdog.png'
},
{
name: 'milkshake',
img: 'images/milkshake.png'
},
{
name: 'pizza',
img: 'images/pizza.png'
},
{
name: 'ice-cream',
img: 'images/ice_cream.png'
}
]
cardArray.sort(() => 0.5 - Math.random())
const gridDisplay = document.querySelector('#grid')
const resultDisplay = document.querySelector('#result')
let cardsChosen = []
let cardsChosenIDs = []
const cardsWon = []
function createBoard(){
for (let i = 0; i < cardArray.length; i++){
const card = document.createElement('img')
card.setAttribute('src', 'images/blank.png')
card.setAttribute('data-id', i)
card.addEventListener('click', flipCard)
gridDisplay.append(card)
}
}
createBoard()
function checkMatch(){
const cards = document.querySelectorAll('img') // or '#gird img'
const optionOneId = cardsChosenIDs[0]
const optionTwoId = cardsChosenIDs[1]
//console.log(cards)
//console.log('check for match!')
if (optionOneId == optionTwoId){
cards[optionOneId].setAttribute('src', 'images/blank.png')
cards[optionTwoId].setAttribute('src', 'images/blank.png')
//alert('You have clicked the same images!')
}
else if (cardsChosen[0] == cardsChosen[1]){
//alert('You found a match!')
cards[optionOneId].setAttribute('src', 'images/black.png')
cards[optionTwoId].setAttribute('src', 'images/black.png')
cards[optionOneId].removeEventListener('click', flipCard) // remove flipCard function once matched
cards[optionTwoId].removeEventListener('click', flipCard)
cardsWon.push(cardsChosen)
}
else {
cards[optionOneId].setAttribute('src', 'images/blank.png')
cards[optionTwoId].setAttribute('src', 'images/blank.png')
//alert('Oh oh! Try again!')
}
resultDisplay.textContent = cardsWon.length
cardsChosen = []
cardsChosenIDs = []
if (cardsWon.length == (cardArray.length / 2)){ // = 6
resultDisplay.textContent = "Woo Hoo! You beat the game."
}
}
function flipCard(){
console.log(cardArray)
const cardId = this.getAttribute('data-id')
//console.log(cardArray[cardId].name)
cardsChosen.push(cardArray[cardId].name)
cardsChosenIDs.push(cardId)
//console.log('clicked', cardId)
console.log(cardsChosen)
console.log(cardsChosenIDs)
this.setAttribute('src', cardArray[cardId].img)
if (cardsChosen.length === 2) {
setTimeout(checkMatch, 500)
}
}