const array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
const string = "abcdefghijklmnopqrstuvwxyz";
// 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)]);
const months = ["January", "February", "March", "April", "May", "June", "July"];
const random = Math.floor(Math.random() * months.length);
console.log(random, months[random]);
const months = ["January", "February", "March", "April", "May", "June"];
const random = Math.floor(Math.random() * months.length);
console.log(random, months[random]);
//Make all arrays have "random" method
Array.prototype.random = function() {
return this[Math.floor(Math.random() * this.length)];
}
//Call "random" method on an array
var result = ["Hello", "world"].random();
const randomItem = items[Math.floor(Math.random() * items.length)];
// Define random() method of Arrays
Array.prototype.random = function() {
return this[Math.floor(Math.random() * this.length)];
}
console.log([1, 2, 3, 4, 5, 6].random()); // 5 for example
console.log(['apple', 'banana', 'orange'].random()); // 'orange' for example