xxxxxxxxxx
var colors = ["red","blue","green","yellow"];
var randomColor = colors[Math.floor(Math.random()*colors.length)]; //pluck a random color
xxxxxxxxxx
//get random value from array
var colors = ["red","blue","green","yellow"];
var randColor = colors[Math.floor(Math.random() * colors.length)];
xxxxxxxxxx
var myArray = [
"Apples",
"Bananas",
"Pears"
];
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
xxxxxxxxxx
var items = ['Yes', 'No', 'Maybe'];
var item = items[Math.floor(Math.random() * items.length)];
xxxxxxxxxx
var foodItems = ["Bannana", "Apple", "Orange"];
var theFood = foodItems[Math.floor(Math.random() * foodItems.length)];
/* Will pick a random number from the length of the array and will go to the
corosponding number in the array E.G: 0 = Bannana */
xxxxxxxxxx
const array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
const string = "abcdefghijklmnopqrstuvwxyz";
// `Math.floor` can be replaced with `~~`
// random item from Array
console.log(array[Math.floor(Math.random() * array.length)]);
// random Char from String
console.log(string[Math.floor(Math.random() * string.length)]);
xxxxxxxxxx
function RandomItemFromArray(myArray) {
return myArray[Math.floor(Math.random() * myArray.length)]
}
var fruit = [ "Apples", "Bananas", "Pears", ];
let fruitSample = RandomItemFromArray(fruit)
xxxxxxxxxx
const months = ["January", "February", "March", "April", "May", "June"];
const random = Math.floor(Math.random() * months.length);
console.log(random, months[random]);