Last active
December 13, 2016 16:09
-
-
Save Anthodpnt/96a8bcee819b0fe87ee19abb31459f15 to your computer and use it in GitHub Desktop.
Math - Random Array Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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