-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (135 loc) · 3.7 KB
/
app.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
document.addEventListener("DOMContentLoaded", () => {
//card options
let cardArray = [
{
name: "fries",
img: "images/fries.png",
},
{
name: "cheeseburger",
img: "images/cheeseburger.png",
},
{
name: "ice-cream",
img: "images/ice-cream.png",
},
{
name: "pizza",
img: "images/pizza.png",
},
{
name: "milkshake",
img: "images/milkshake.png",
},
{
name: "hotdog",
img: "images/hotdog.png",
},
{
name: "fries",
img: "images/fries.png",
},
{
name: "cheeseburger",
img: "images/cheeseburger.png",
},
{
name: "ice-cream",
img: "images/ice-cream.png",
},
{
name: "pizza",
img: "images/pizza.png",
},
{
name: "milkshake",
img: "images/milkshake.png",
},
{
name: "hotdog",
img: "images/hotdog.png",
},
];
let renderedCards = [];
// Easy Sort
cardArray.sort(() => 0.5 - Math.random());
const grid = document.querySelector(".grid");
const resultDisplay = document.querySelector("#result");
var cardsChosen = [];
var cardsChosenId = [];
const cardsWon = [];
//create your board
function createBoard() {
// Diffcult sort
grid.innerHTML = "";
shuffle(cardArray);
for (let i = 0; i < cardArray.length; i++) {
let card = document.createElement("img");
card.setAttribute("class", ".griditem");
card.setAttribute("src", "images/blank.png");
card.setAttribute("data-id", i);
card.addEventListener("click", flipCard);
grid.appendChild(card);
}
}
//check for matches
function checkForMatch() {
renderedCards = document.querySelectorAll("img");
const optionOneId = cardsChosenId[0];
const optionTwoId = cardsChosenId[1];
if (cardsChosen[0] === cardsChosen[1]) {
cardsChosen = [];
cardsChosenId = [];
alert("You found a match");
renderedCards[optionTwoId].setAttribute("src", "images/white.png");
renderedCards[optionOneId].setAttribute("src", "images/white.png");
//diffcult
//Set the two aside from the pick list.
cardArray.splice(renderedCards[optionOneId].getAttribute("data-id"), 1);
cardArray.splice(renderedCards[optionTwoId].getAttribute("data-id"), 1);
cardsWon.push(cardsChosen);
// display the win count
resultDisplay.textContent = cardsWon.length;
} else {
cardsChosen = [];
cardsChosenId = [];
//Flip the two back as before.
renderedCards[optionOneId].setAttribute("src", "images/blank.png");
renderedCards[optionTwoId].setAttribute("src", "images/blank.png");
alert("Sorry, try again");
}
if (cardsWon.length === cardArray.length / 2) {
resultDisplay.textContent = "Congratulations! You found them all!";
} else {
// Difficult
createBoard();
}
}
//flip your card
function flipCard() {
let cardId = this.getAttribute("data-id");
cardsChosen.push(cardArray[cardId].name);
cardsChosenId.push(cardId);
this.setAttribute("src", cardArray[cardId].img);
if (cardsChosen.length === 2) {
setTimeout(checkForMatch, 500);
}
}
createBoard();
function shuffle(array) {
var currentIndex = array.length,
temporaryValue,
randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
});