Math.random()
to generate a random number.Array.prototype.length
and round it off to the nearest whole number using Math.floor()
.const sample = arr => arr[Math.floor(Math.random() * arr.length)];
sample([3, 7, 9, 11]); // 9
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️