DEV Community

Discussion on: What the Hex!? (how to generate random hex color codes in javascript)

Collapse
 
xanderyzwich profile image
Corey McCarty

You could also scale your random by the max value #ffffff and you wouldn't need a loop.

Collapse
 
thecodepixi profile image
Emmy | Pixi

Someone else pointed this out, and it does work quite often, but you do still need to check for a length of 6 on your output for it to be a valid hex color code. Testing that code, it does occasionally output numbers of 4 or 5 digits in length which will break the intended functionality.

Collapse
 
xanderyzwich profile image
Corey McCarty

I believe that there is a function to pad the left side of the string with zeroes up to a given length (or you could do it yourself)