Skip to content

Instantly share code, notes, and snippets.

@Anthodpnt
Last active December 13, 2016 16:09
Show Gist options
  • Select an option

  • Save Anthodpnt/96a8bcee819b0fe87ee19abb31459f15 to your computer and use it in GitHub Desktop.

Select an option

Save Anthodpnt/96a8bcee819b0fe87ee19abb31459f15 to your computer and use it in GitHub Desktop.
Math - Random Array Node
/**
* This gist is for Javascript beginners.
* @author: Anthony Du Pont <[email protected]>
* @site: https://www.twitter.com/JsGists
*
* The idea is to get a random node/entry from a given array.
*
* Example:
* I can't choose which fruit I'll eat this afternoon, they all look so tasty so I'll simply close my eyes
* and choose one of them randomly.
**/
const fruits = ["banana", "apple", "strawberries", "orange"];
const random = randomArrayNode(fruits); // Return a random fruit from the array (apple, orange, banana,...).
function randomArrayNode(array) {
return array[Math.random() * array.length | 0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment