DEV Community

Discussion on: Daily Challenge #158 - RGB To Hex Conversion

Collapse
 
savagepixie profile image
SavagePixie • Edited

My JavaScript solution, which would look a lot nicer if the TC39 finally added a pipeline operator.

const checkRange = num => Math.min(255, Math.max(num, 0))

const rgb = (...args) => args.slice(0, 3)
    .map(x => checkRange(x)
        .toString(16)
        .toUpperCase()
        .padStart(2, '0'))
    .join('')
Enter fullscreen mode Exit fullscreen mode
Collapse
 
avalander profile image
Avalander

We have padStart now in strings to add characters to the beginning up to a certain length.

Collapse
 
lffg profile image
Luiz Felipe Gonçalves • Edited

String.prototype.padStart would handle the addZeroes trick for you. ;)

Collapse
 
quozzo profile image
Quozzo • Edited

I got really confused when the link opened in Portuguese. I had to check my VPN 😆

Thread Thread
 
lffg profile image
Luiz Felipe Gonçalves

Whoops! Sorry about that. :P Should be fixed now.

Collapse
 
savagepixie profile image
SavagePixie • Edited

I've modified it to use padStart instead of my custom function.

Thanks @lffg and @avalander !