slanted-gamedev-toolz
Version:
A slanted mix of tools for your brilliant ideas in game design.
16 lines (15 loc) • 467 B
JavaScript
/**
* Returns a random item from the provided array.
*
* @param {any[]} array - The array to pick a random item from.
* @returns {any} A random element from the array.
*
* @example
* const fruits = ['apple', 'banana', 'cherry'];
* const randomFruit = randomArrayItem(fruits);
* console.log(randomFruit); // e.g., 'banana'
*/
export const randomArrayItem = (array) => {
const index = Math.floor(Math.random() * array.length);
return array[index];
};