I have a little script that I’ve written to generate a random color hex code. It goes like this:
function genHex() {
colors = new Array(14)
colors[0] = '0'
colors[1] = '1'
colors[2] = '2'
colors[3] = '3'
colors[4] = '4'
colors[5] = '5'
colors[6] = '6'
colors[7] = '7'
colors[8] = '8'
colors[9] = '9'
colors[10] = 'a'
colors[11] = 'b'
colors[12] = 'c'
colors[13] = 'd'
colors[14] = 'e'
colors[15] = 'f'
digit = new Array(5)
color = ""
for (i = 0; i < 6; i++) {
digit[i] = colors[Math.round(Math.random() * 14)]
color = color + digit[i]
}
return color;
}
I know this isn’t the prettiest way to accomplish what I’m doing, but the means will ultimately serve a different end for a more complex project. Regardless, when I include this genhex.js file alongside the latest Jquery, nothing works!
For instance,
$().ready(function() {
alert(genhex());
});
does nothing. What am I missing here? Is there some reason that color not a string in this case?
Try:
To call
genHex()on document ready.Also the function is defined as
genHex()but you’re trying to call it asgenhex(). Function names are case-sensitive in JavaScript.