logo CodeStepByStep logo

rgbToHexColor

Language/Type: JavaScript loops string parameters hexadecimal rgb
Author: Melissa Hovik (on 2018/10/22)

Write a function named rgbToHexColor that takes an rgb color code string in the format of "rgb(r, g, b)" and returns the equivalent hexadecimal code string in the format of "#RRGGBB" where "r", "g", "b" are the integer values corresponding to the hexidecimal values "RR", "GG", "BB" respectively. Recall that the range of a two-digit hexidecimal code is "00" to "FF" which corresponds to the integer range 0 to 255, inclusive ("F" has a decimal value of 16).

For example, the call rgbToHexColor("rgb(0, 0, 0)") should return the string "#000000" and the call rgbToHexColor("rgb(192, 255, 238)") should return the string "#C0FFEE".

You may assume the passed string is in the specified rgb string format (maintaining casing and spacing) and each rgb value in the string is an integer value between 0 and 255 (inclusive).

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.