var myName = ['Beth', 'Chris', 'Carl', 'April', 'Cinnamon','Ethan'];
I want to be able to pick a number or name from an array and use that same value again? I am using javascript
var myName = ['Beth', 'Chris', 'Carl', 'April', 'Cinnamon','Ethan'];
I want to be able to pick a number or name from an array and use that same value again? I am using javascript
You can use random Math function. If your array has 6 elements. you can use the below JavaScript.
myName[Math.floor(Math.random()*6)]
Why would something simple such as this not work. Up to your to explore the truly "random" aspect of this but likely OK for most things. Note that arrays are 0 based thus the 0,myName.length-1
parameters.
function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
var myName = ['Beth', 'Chris', 'Carl', 'April', 'Cinnamon','Ethan'];
var randomName = myName[randomIntFromInterval(0,myName.length-1)];
console.log(randomName);