1

I am new in Javascript. I want to create a function which will take a hexadecimal color code and it will convert it to rgb(xx,xx,xx). My code is found below

function my(a){
var first = a[1] + a[2];
var second = a[3] + [4];
var third = a[5] + a[6];
var res = [first,second,third];
for(var i = 0; i <res.length; i++){
res[i] = parseInt(res[i],16);
}
return res;
}

First and third array element is converting correctly except second array element. I don't why, can anyone tell me why?

Mestica
  • 1,489
  • 4
  • 23
  • 33

1 Answers1

1

You had a typo in assigning second. You had a[3] + [4], when it should be a[3] + a[4].

function my(a) {
    var first = a[1] + a[2];
    var second = a[3] + a[4];
    var third = a[5] + a[6];
    var res = [first,second,third];
    for(var i = 0; i < res.length; i++){
        res[i] = parseInt(res[i],16);
    }
    return res;
}
manonthemat
  • 6,101
  • 1
  • 24
  • 49