My JavaScript memory game is functioning properly, but I would like the images to load in a random order. Unfortunately, I'm not sure how to accomplish this.
Here is the current setup:
var easyImages = ["img/bat.jpg", "img/bug.jpg", "img/cat.jpg", "img/dog.jpg",
"img/bat.jpg", "img/bug.jpg", "img/cat.jpg", "img/dog.jpg"];
var hardImages = ["img/bat.jpg", "img/bug.jpg", "img/cat.jpg", "img/dog.jpg",
"img/frog.jpg", "img/fly.jpg", "img/bat.jpg", "img/bug.jpg",
"img/cat.jpg", "img/dog.jpg", "img/frog.jpg", "img/fly.jpg"]
var imagesToShow;
var imagesContainer = document.getElementById("images-container");
var questionMark = "img/memory-bg.jpg";
if (selectedDifficulty == "easy") {
imagesToShow = easyImages;
}
else if (selectedDifficulty == "hard") {
imagesToShow = hardImages;
}
imagesContainer.innerHTML = "";
if (imagesToShow !== "") {
for (var i = 0; i < imagesToShow.length; i++) {
var img = document.createElement("img");
img.addEventListener("click", flipImage);
img.src = questionMark;
img.dataset.img = imagesToShow[i];
imagesContainer.appendChild(img);
}
}