-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
109 lines (101 loc) · 3.01 KB
/
script.js
File metadata and controls
109 lines (101 loc) · 3.01 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
const gameToys = [
{
name: 'cheeseburger',
imgPath: 'images/cheeseburger.png'
},
{
name: 'fries',
imgPath: 'images/fries.png'
},
{
name: 'hotdog',
imgPath: 'images/hotdog.png'
},
{
name: 'icecream',
imgPath: 'images/icecream.png'
},
{
name: 'milkshake',
imgPath: 'images/milkshake.png'
},
{
name: 'pizza',
imgPath: 'images/pizza.png'
},
{
name: 'cheeseburger',
imgPath: 'images/cheeseburger.png'
},
{
name: 'fries',
imgPath: 'images/fries.png'
},
{
name: 'hotdog',
imgPath: 'images/hotdog.png'
},
{
name: 'icecream',
imgPath: 'images/icecream.png'
},
{
name: 'milkshake',
imgPath: 'images/milkshake.png'
},
{
name: 'pizza',
imgPath: 'images/pizza.png'
}
]
gameToys.sort( () => 0.5-Math.random())
const gameToysContainer = document.querySelector('.playing-area')
let selectedCardsId = []
let selectedCardsName = []
let collectingCards = []
function addingBlanks(){
for (let index = 0; index < gameToys.length; index++) {
const imgElement = document.createElement('img')
imgElement.setAttribute('src', 'images/blank.png')
imgElement.setAttribute('data-id', index)
imgElement.addEventListener('click', selectedChoice)
gameToysContainer.append(imgElement)
}
}
function verifyCards(){
const imgSelector = document.querySelectorAll('img')
const selectOne = imgSelector[selectedCardsId[0]]
const selectTwo = imgSelector[selectedCardsId[1]]
if(selectedCardsId[0] == selectedCardsId[1]){
alert('YOU HAVE SELECTED THE SAME CARD...')
imgSelector[selectedCardsId[0]].setAttribute('src', 'images/blank.png')
}
else if(selectedCardsName[0] === selectedCardsName[1]){
alert('YOU HAVE FOUND A MATCH ! ! !')
selectOne.setAttribute('src', 'images/white.png')
selectTwo.setAttribute('src', 'images/white.png')
selectOne.removeEventListener('click', selectedChoice)
selectTwo.removeEventListener('click', selectedChoice)
collectingCards.push(selectedCardsName)
}
else{
selectOne.setAttribute('src', 'images/blank.png')
selectTwo.setAttribute('src', 'images/blank.png')
alert('OOPS! TRY AGAIN')
}
selectedCardsName = []
selectedCardsId = []
if(collectingCards.length === gameToys.length/2){
gameToysContainer.innerHTML = `<p> YOU WON THE GAME </p>`
}
}
function selectedChoice(){
let selectedElement = this.getAttribute('data-id')
selectedCardsName.push(gameToys[selectedElement].name)
selectedCardsId.push(selectedElement)
this.setAttribute('src', gameToys[selectedElement].imgPath)
if(selectedCardsId.length === 2){
setTimeout(verifyCards, 500)
}
}
addingBlanks()