So, I have a memory game written, but was wondering how I could turn these letters in the cardSet into images (I have a set of images in an images folder within the folder containing this code, called cat.png, cow.png, etc). Here's the js:
$(document).ready(function () {
var cardSet = ["A", "A", "B", "B", "C", "C", "D", "D",
"E", "E", "F", "F", "G", "G", "H", "H"];
randomize(cardSet);
load(cardSet);
var tries = 0;
var timer;
var pair = 0;
var $box1 = undefined;
var $box2 = undefined;
$("button").click(function () {
if ($(this).attr("class") == "hidden") {
if ($box1 == undefined) {
$box1 = $(this);
$box1.removeClass("hidden").addClass("show");
timer = setTimeout(function () {
if ($box2 == undefined) {
$box1.removeClass("show").addClass("hidden");
$box1 = undefined;
numberOfClicks += 1;
} else if ($box1.text() != $box2.text()) {
$box1.removeClass("show").addClass("hidden");
$box1 = undefined;
$box2.removeClass("show").addClass("hidden");
$box2 = undefined;
tries += 1;
}
}, 3000);
} else if ($box2 == undefined) {
$box2 = $(this);
$box2.removeClass("hidden").addClass("show");
if ($box1.text() == $box2.text()) {
pair += 1;
tries += 1;
$box1 = undefined;
$box2 = undefined;
};
};
};
if (pair == 8) {
alert("Tries: " + tries);
}
});
});
function load(cardSet) {
var index = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
$("#b" + i + j).text(cardSet[index]);
index++;
}
}
}
function randomize(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
table {
margin-left: auto;
margin-right: auto;
border-collapse: collapse;
border: 1px solid;
}
table td {
border: 1px solid;
height: 100px;
width: 100px;
text-align: center;
}
button {
border: 0px solid;
height: 100px;
width: 100px;
text-align: center;
font-size: 150%;
font-weight: 300;
font-family: "Questrial", sans-serif;
color: #000000;
background-color: #FFFFFF;
}
.hidden {
opacity: 0;
}
.show {
opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width = "500px" height = "500px">
<tr>
<td><button id="b00" class="hidden"></button></td>
<td><button id="b01" class="hidden"></button></td>
<td><button id="b02" class="hidden"></button></td>
<td><button id="b03" class="hidden"></button></td>
</tr>
<tr>
<td><button id="b10" class="hidden"></button></td>
<td><button id="b11" class="hidden"></button></td>
<td><button id="b12" class="hidden"></button></td>
<td><button id="b13" class="hidden"></button></td>
</tr>
<tr>
<td><button id="b20" class="hidden"></button></td>
<td><button id="b21" class="hidden"></button></td>
<td><button id="b22" class="hidden"></button></td>
<td><button id="b23" class="hidden"></button></td>
</tr>
<tr>
<td><button id="b30" class="hidden"></button></td>
<td><button id="b31" class="hidden"></button></td>
<td><button id="b32" class="hidden"></button></td>
<td><button id="b33" class="hidden"></button></td>
</tr>
</table>
I apologize for the messy code... Fairly new to javascript. Any advice is accepted though!