Thanks for the credit!
About this post, something that might be useful for you: To pad zeroes to a string, you can use the native String.prototype.padStart instead of concatenating "0" and then slicing. Here are the examples:
String.prototype.padStart
"0"
For method 4:
function getRandomColor() { function c() { return Math.floor(Math.random() * 256) .toString(16) .padStart(2, "0"); } return "#" + c() + c() + c(); }
And if we take it one step further:
const getRandomHex = () => Math.floor(Math.random() * 0xff) .toString(16) .padStart(2, "0"); const getRandomColor = () => `#${getRandomHex()}${getRandomHex()}${getRandomHex()}`;
For method 5:
function getRandomColor() { return "#" + Math.random().toString(16).slice(2, 8).padStart(6, "0"); }
And also taking it one step further:
const getRandomColor = () => `#${Math.random().toString(16).slice(2, 8).padStart(6, "0")}`;
Cheers!
Okay great, well that's the benefit of an active community, you learn everyday! And your comments just enhance my article thanks for this and Cheers!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for the credit!
About this post, something that might be useful for you: To pad zeroes to a string, you can use the native
String.prototype.padStartinstead of concatenating"0"and then slicing. Here are the examples:For method 4:
And if we take it one step further:
For method 5:
And also taking it one step further:
Cheers!
Okay great, well that's the benefit of an active community, you learn everyday!
And your comments just enhance my article thanks for this
and Cheers!