Skip to content

Instantly share code, notes, and snippets.

@eszterkv
Created July 14, 2016 12:16
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eszterkv/9ad0c77f2713de58fa1c4c0e74199d33 to your computer and use it in GitHub Desktop.
Save eszterkv/9ad0c77f2713de58fa1c4c0e74199d33 to your computer and use it in GitHub Desktop.
A JavaScript solution for random choice from an array.
function randomChoice(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}

randomchoice.js

A JavaScript solution for random choice from an array.

Usage

Pass any array to the function, it returns a randomly chosen item of that array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment