I'm making a javascript memory game and it's actually working, but I want the images to load in a random order. It's just that I have no idea how to.
This is what it currently looks like:
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);
}
}