DEV Community

Discussion on: Get your first dev job by building these projects! #1: Guess the HEX Code Game

Collapse
 
arthurcjp profile image
Arthur Hudson

That's an amazing post, easy to follow and really constructive. Thank you for writing this, instead of just making the videos and thanks for sharing your knowledge with us

Collapse
 
catmcgeecode profile image
Cat McGee

Thank you so much! Hope it helps you ☺️

Collapse
 
arthurcjp profile image
Arthur Hudson

Oh hey, this is me again. Look, I did some tests, and, sometimes, instead of a 6 digits hexcode, the '#' + Math.floor(Math.random() * 16777215).toString(16); returns a 5 digits hexcode, what "breaks" the code, and give no background.

To solve this, I did the following code

function hexColorGenerator() {
var generatedColor = "";

for ( i = 0; i < 3; i++) {
    var randomNumber = Math.floor(Math.random()*255);
    if (randomNumber < 16) {
        generatedColor = generatedColor + "0" + randomNumber.toString(16);
    } else {
        generatedColor = generatedColor + randomNumber.toString(16)
    }
}

return (generatedColor);

}

Thread Thread
 
catmcgeecode profile image
Cat McGee • Edited

Nice one! All of the projects in my tutorials are super basic and an be optimized to handle edge cases like this. Super awesome that you're doing that!