-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq3.js
40 lines (30 loc) · 1.09 KB
/
q3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* QUESTION 3:
Write a function that converts HEX to RGB.
Then Make that function auto-dect the formats so that
if you enter HEX color format it returns RGB and
if you enter RGB color format it returns HEX.
*/
// Test values
let hexVal1 = "9e";
let hexVal2 = "4a";
let hexVal3 = "4a"; // rgb(158, 74, 74)
let rgbVal1 = 255;
let rgbVal2 = 104;
let rgbVal3 = 23; // #ff6817
function colourConverter(val,val2,val3) { // hex to rgb, rgb to hex
if(typeof val === "string" && typeof val2 === "string" && typeof val3 === "string") {
val = parseInt(val, 16);
val2 = parseInt(val2, 16);
val3 = parseInt(val3, 16);
return `rgba(${val}, ${val2}, ${val3})`;
} else if (typeof val === "number" && typeof val2 === "number" && typeof val3 === "number") {
val = val.toString(16);
val2 = val2.toString(16);
val3 = val3.toString(16);
return `#${val}${val2}${val3}`;
}
}
console.log("////// START OF Q3 //////");
console.log("Q3 vals RGB =>", colourConverter(hexVal1,hexVal2,hexVal3));
console.log("Q3 vals HEX =>", colourConverter(rgbVal1,rgbVal2,rgbVal3));
console.log("////// END OF Q3 //////");