-
Notifications
You must be signed in to change notification settings - Fork 0
/
memG.js
126 lines (108 loc) · 2.97 KB
/
memG.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
const playBoard = document.getElementById("grid");
const scoreResult = document.getElementById("result");
scoreResult.innerText = "0";
let cardsClicked = [];
let cardClickedId = [];
let cardsWon = [];
const cards = [
{
name: "pizza",
imgSrc :'images/pizza.png'
},
{
name: "cheeseburger",
imgSrc :'images/cheeseburger.png'
},
{
name: "fries",
imgSrc :'images/fries.png'
},
{
name: "hotdog",
imgSrc :'images/hotdog.png'
},
{
name: "milkshake",
imgSrc :'images/milkshake.png'
},
{
name: "ice-cream",
imgSrc :'images/ice-cream.png'
},
{
name: "pizza",
imgSrc :'images/pizza.png'
},
{
name: "cheeseburger",
imgSrc :'images/cheeseburger.png'
},
{
name: "fries",
imgSrc :'images/fries.png'
},
{
name: "hotdog",
imgSrc :'images/hotdog.png'
},
{
name: "milkshake",
imgSrc :'images/milkshake.png'
},
{
name: "ice-cream",
imgSrc :'images/ice-cream.png'
}
];
cards.sort(()=> 0.5 - Math.random());
// Board initial setup //
function initialBoard(){
for(let i=0; i < 12; i++){
const ele = document.createElement('img');
ele.setAttribute('src', 'images/blank.png');
ele.setAttribute('id', i);
ele.setAttribute('class', 'card')
ele.addEventListener('click', cardReveal);
playBoard.append(ele);
}
}
// Card Matching Check code //
function checkMatch(){
const imgEle = document.querySelectorAll("img");
let optionOne = cardClickedId[0];
let optionTwo = cardClickedId[1];
if(optionOne === optionTwo){
imgEle[optionOne].setAttribute('src','images/blank.png');
imgEle[optionTwo].setAttribute('src','images/blank.png');
alert("you clicked the same card")
} else if(cardsClicked[0] === cardsClicked[1]){
alert("Cards Matched!, Keep it up");
imgEle[optionOne].setAttribute('src','images/white.png');
imgEle[optionTwo].setAttribute('src','images/white.png');
imgEle[optionOne].removeEventListener('click',cardReveal);
imgEle[optionTwo].removeEventListener('click',cardReveal);
cardsWon.push(cardsClicked);
scoreResult.innerText = `${cardsWon.length}`
} else{
imgEle[optionOne].setAttribute('src','images/blank.png');
imgEle[optionTwo].setAttribute('src','images/blank.png');
alert("sorry try again");
}
cardClickedId = [];
cardsClicked = [];
if(cardsWon.length === 6){
alert("Congratulations you have won")
}
}
// Card Flip Event //
function cardReveal(){
const cardId = this.getAttribute('id');
cardsClicked.push(cards[cardId].name);
cardClickedId.push(cardId);
this.setAttribute('src',cards[cardId].imgSrc);
if(cardsClicked.length === 2){
setTimeout(checkMatch,500);
}
;
}
initialBoard();