samedi 26 mars 2016

JS: Randomly cycling through array of images

I have an array of images that are randomly placed in a table and need to cycle through the images on the top left corner piece of the table randomly as well. The idea is that every time the button is clicked, the image randomly changes to another from the array. I am inserting my code here:

var imageList = new Array("img0.jpg", "img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg", "img6.jpg", "img7.jpg", "img8.jpg", "img9.jpg", "img10.jpg", "img11.jpg");

    function pick()
    {
        var newImageLoad='';
        newPick = Math.floor(Math.random()*imageList.length);
        newImageLoad = imageList[newPick];
        return newImageLoad;
    }
    function LoadNew()
    {
      document.getElementById("cell0").src = pick();
    }

The issue I am having is that for some reason it keeps pulling an undefined image every click instead of one from the array. Why is this?




Aucun commentaire:

Enregistrer un commentaire