I’m relatively new to javascript. I’m looking for a javascript function to get an arbitrary element of a given array based on a given string input.
A random(seed) function would be useful but I can’t find one.
Or maybe a way to xor hash the bytes in a string.
I can’t use jquery, and I’m hoping for an answer that’s relatively short.
Edit for examples:
var myArray = ["foo", "bar", "24", "asdf"];
the signature would look like
function ArbitraryElement(arr, seed)
and outputs would look something like:
ArbitraryElement(myArray, "what") // "foo"
ArbitraryElement(myArray, "that") // "24"
ArbitraryElement(myArray, "what") // "foo"
ArbitraryElement(myArray, "boo") // "24"
ArbitraryElement(myArray, "cat") // "asdf"
// etc...
You could sum the char codes with reduce, IE9 native and IE8 and below with shim included in link:
EDIT:
Here’s an update: http://jsfiddle.net/sqA6Z/