mercredi 30 novembre 2016

Simple javascript for selecting random element out of array outputting "undefined" on 2 elements (jsfiddle included)

Below is my code, for some reason it outputs undefined for ['6'] & ['7'], all the other ones work. I don't understand what is going wrong.

var array = [
    ['1'],
    ['2'],
    ['3'],
    ['4'],
    ['5'],
    ['6']
    ['7'],
    ['8'],
    ['9'],
    ['10']
];

if(document.getElementById("random-element")) {
    var rand = array[Math.floor(Math.random() * array.length)];
    document.getElementById('random-element').innerHTML = rand;
}

http://ift.tt/2fDrvjB




Aucun commentaire:

Enregistrer un commentaire