Your code is erroneous. Array.length refers to the number of arguments the Array constructor function is defined with. What you want is a.length, b.length, etc.
You should also use Math.floor, not Math.round. Flooring gives [0, length), whereas rounding gives [0, length].
Code:
if(!Array.prototype.map)
Array.prototype.map = function(f /*, context */) {
for(var r = [], i = 0, n = this.length, t = arguments[1]; i < n; ++i)
r[i] = f.call(t, this[i], i, this);
return r;
};
function generate() {
document.getElementById("message").innerHTML = [
"If",
["Santa Claus", "Britney Spears", "Fish E. Guy", "Batman", "Al Gore", "Mickey Mouse", "Eli Manning"],
["ruled the world", "was US president"],
"there would be",
["more", "less"],
["explosions.", "donuts.", "dolphin rebellions.", "99% taxes", "EVIL!!!"]
].map(function(a) {
return typeof a != "string"
? a[Math.floor(Math.random() * a.length)]
: a;
}).join(' ');
}
Bookmarks