AniUI Academy
easy+150 XPPractice

Format RGB channels as hex

Colour pickers and interpolation code work in numbers, but CSS and <input type="color"> want #rrggbb. This conversion is where off-by-one padding bugs live: channel 1 must render as 01, not 1.

Implement rgbToHex(r, g, b). Each channel is first rounded with Math.round, then clamped into the range 0 to 255, then rendered as exactly two lowercase hex digits, zero-padded on the left.

Return the three channels concatenated after a single #, so the result is always exactly seven characters, lowercase — for example "#ff8800".

What it has to do

  • Return a seven character string starting with #, using lowercase hex digits.
  • Zero-pad each channel to two digits.
  • Round fractional channel values with Math.round.
  • Clamp values below 0 to 0 and above 255 to 255.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.