lundi 26 février 2018

How to alert a single element from an array in javascript?

Hello everyone I am new here and I am just starting to learn javascript.

I need to come up with a piece off code that alerts a single random element from an array. I have managed to come up with the random element but it alerts all ten of them from the array. While I need t to alert a single one and stop.

This is what I have so far.

    var myList = new Array("gusty", "long", "tremendous", "play", "white", 
    "literate", "baboon", "dusty", "government", "cheer");

    for(i = 0; i < myList.length; i++)
    {
        var y = Math.floor(Math.random() * 10);
        alert(myList[y]);
    }

So when I load this in a browser I get ten random words. But I just want one. I tired putting the var inside the alert but that didn't do anything.




Aucun commentaire:

Enregistrer un commentaire