Created
May 17, 2011 06:29
-
-
Save samyakbhuta/976044 to your computer and use it in GitHub Desktop.
randomInArray
This file contains 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
/* | |
* Returns a random element in a function. | |
*/ | |
var randomInArray = function (anArray){ | |
//TODO : Check if anArray is really an array. | |
var randomIndex=Math.floor(Math.random()*anArray.length) | |
return anArray[randomIndex]; | |
} | |
//Usage | |
myArray = ["a","b","c","d"]; | |
randomInArray(myArray); // Will be any of "a","b","c" or "d" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment