Sometimes it’s helpful to get a random item from an array. Here’s how.
Here’s the code:
function randomItem(items) {
if (!Array.isArray(items)) {
throw "Argument must be an array: " + items;
}
if (items.length == 0) {
throw "Array argument is empty";
}
return items[Math.floor(Math.random() * items.length)];
}