For my most recent Flatiron School project, I wanted to be able to programmatically change the background color of one of the elements on the page,...
For further actions, you may consider blocking this person and/or reporting abuse
Nice work! I love seeing the thought processes behind figuring out how to make code better. It's like watching someone piece together a mystery in front of you.
I'm sure you know this as well, but for anyone who doesn't, if you prefix a number with
0xyou can use hex values in your code. (There's also0bfor binary and0ofor octal).Something else to note is that when using Math.floor1 since
Math.randomgenerates a random number between 0 and 1, but never exactly 1, you have to add 1 to your maximum or else it will never generate that value. So in this case, you should go up to16or you'll never get the valuef:1 I didn't read the article well enough and assumed
Math.floorwas being usedThanks, Khauri! I actually didn't know about the
0xthing. That's super cool! And ++ to the Math.floor() point. It's one of those things I always forget. I used Math.round() in its place but do know that that's not the best solution.Yeah someone else pointed this out to me. Math.floor vs Math.round is one of those things I always forget. Also, excellent work on the alt method! Just as a note, with that solution, it is possible you will get hex codes that are less than 6 digits in length, which won't work as color codes. You'll still need a way to check for the length of the code.
Just wanted to say I am proud of you for writing this post!
It is great and I must go do something similar now 😂 maybe with cats 🤔
I demand colorful cats!
O M G !!! How do I do colorful cats? Idk but I must find out!
I have no idea but I believe in your ability to figure it out haha
Thank you for your trust! 🌈😽😼
You could also scale your random by the max value #ffffff and you wouldn't need a loop.
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.
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)
so fun
I know, right!? I want to sit and click that lil square on codepen forever haha
Just to clarify, in the article, the intention is to generate individual digits and convert them to hex one at a time until you have a string of 6 hexadecimal digits. If you just generate any decimal number between 0 and 16777215 and then convert that number to hexadecimal, it will sometimes output hexadecimal numbers shorter than 6 digits (i tested and got a few 4 and 5 digit results) which cannot be used as a hex color code and will cause the function to break if used to assign a code to an element on the page. You still need a way to check for length of 6 on your output. That said, your code does work a lot of the time. But sometimes it doesn't. For me, it broke about 1/20 times when I tested it.
Very Nice!
But, I could see the function is excluding the value
fevery time. Do you have a fix for that?Your code is shorter but harder to understand.