DEV Community


Discussion on: And then the interviewer asks, "Can you do this with less code?"

jpantunes profile image
JP Antunes

I really like one-liners, reminds me of how fun it was working with Perl way back when :-)

Here's one that I came up with a couple of days ago:
How to convert RGB values to a capitalised hex string? Mind you that valid decimal values for RGB are 0 - 255. Any values that fall out of that range must be rounded to the closest valid value.

const rgb = (r, g, b) => Buffer.from(new Uint8ClampedArray([r, g, b])).toString('hex').toUpperCase()
easrng profile image

Golfed, but less checking:

const rgb = (...a) => Buffer.from(new Uint8ClampedArray(a)).toString('hex').toUpperCase()
jpantunes profile image
JP Antunes • Edited

Nice one! Here's a list I made a few months back:

On a second look, your version only shaves off 2 characters and introduces a possible bug imho because you don't control how many parameters are passed to the function, so calling it either less or more than 3 values it will output a wrong value, right?

Forem Open with the Forem app