J. Doe Foe
J. Doe Foe

Reputation: 75

Get a random value from an Array

I have an array consisting of five values as such:

var myArray = ["Hi", "Hello", "Yes", "No", "10"]

I'm trying to get a value printed on random, so i've added this:

var rand = myArray[Math.floor(Math.random() * myArray.length)];

But I'm unsure on where to put it. Anyone got better ideas on how to figure this one out?

Upvotes: 1

Views: 59

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386883

You could assign the random string to an element of DOM.

var myArray = ["Hi", "Hello", "Yes", "No", "10"]
var rand = myArray[Math.floor(Math.random() * myArray.length)];

document.getElementById('out').innerHTML = rand;
<div id="out"></div>

Upvotes: 2

Related Questions