DEV Community

Discussion on: How to Generate random hex color values in javascript

Collapse
 
savagepixie profile image
SavagePixie

Good explanation and good job and making sure to add zeroes!

One small thing, though, pad functions add stuff to a string until the string reaches the desired length. So the ternary there is unnecessary.

let hex = digit().toString(16)

 //check if length is 2, if not pad with 0
hex = hex.length < 2 ? hex.padStart(2, 0) : hex;

The above code could be rewritten as:

let hex = digit().toString(16)
hex = hex.padStart(2, 0)

Or even:

const hex = digit()
    .toString(16)
    .padStart(2, '0')
Collapse
 
enyichiaagu profile image
Amazing Enyichi Agu

Thank you very much. Never even thought of that.