DEV Community

Discussion on: 7 Killer One-Liners in JavaScript

Collapse
 
lexlohr profile image
Alex Lohr

There's an error in generateRandomColor: if math random is below 1/0x100000, the value may not have 6 characters.

// wrong:
const generateRandomHexColor = () =>
  `#${Math.floor(Math.random() * 0xffffff).toString(16)}`;

// corrected:
const generateRandomHexColor = () =>
  `#${Math.floor(0x1000000 + Math.random() * 0xffffff).toString(16).slice(1)}`;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
snigo profile image
Igor Snitkin

Why not just padStart?

Collapse
 
lexlohr profile image
Alex Lohr

String.prototype.padStart is pretty recent and not supported by older browsers.