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!
Latest comments (17)
Nodejs (not web):
Not necessarily actually efficient, but definitely hilarious.
General JS:
My PHP Solution
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)
Python solution, supports 1 or 3 arguments like CSS.
My solution in PHP
Thanks for the tip! I've mostly used string formating for dealing with point numbers in decimals. Need to explore more use cases because the syntax is indeed preferable.
In Go.
I'd do this with Uint8Array
Haskell, using the word8 type to ensure the inputs are from 0-255.
Elm
Test.