The rgb()
method is incomplete. Complete the method so that passing in RGB decimal values will result in a hexadecimal representation being returned.
The valid decimal values for RGB are 0 - 255
. Any (r,g,b) argument values that fall out of that range should be rounded to the closest valid value.
The following are examples of expected output values:
rgb(255, 255, 255) # returns FFFFFF
rgb(255, 255, 300) # returns FFFFFF
rgb(0,0,0) # returns 000000
rgb(148, 0, 211) # returns 9400D3
This challenge comes from jhoffner on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (17)
My JavaScript solution, which would look a lot nicer if the TC39 finally added a pipeline operator.
String.prototype.padStart would handle the
addZeroes
trick for you. ;)I got really confused when the link opened in Portuguese. I had to check my VPN 😆
Whoops! Sorry about that. :P Should be fixed now.
I've modified it to use
padStart
instead of my custom function.Thanks @lffg and @avalander !
We have
padStart
now in strings to add characters to the beginning up to a certain length.Nodejs (not web):
Not necessarily actually efficient, but definitely hilarious.
General JS:
My PHP Solution
Elm
Test.
Haskell, using the word8 type to ensure the inputs are from 0-255.
My solution in PHP
Python solution, supports 1 or 3 arguments like CSS.
In C#, you don't even really need to implement a method for this, as its easily done with string interpolation;
(Useful for unique ad-hoc strings, but usually better factored out to a method anyway)
Ruby solution
In Go.