Since strings are immutable in Javascript, how can I use user's input for conversions?
I was trying to use a function that changes an hexadecimal color code's digits into decimal numbers, but got stuck with the immutable property of strings. I tried copying each character to an array using "for", but the new array was still unoperable. Here is the code sample for what I was trying to do:
var user_input = prompt("Please enter an hexadecimal color code:","e.g. A876FF");
var hexa_to_dec = function(hexa_code)/*changes each digit from hexadecimal to decimal*/
{
for(i = 0; i < hexa_code.length; i++)
{
if (isNaN(hexa_code[i]))
{
switch(hexa_code[i])
{
case "A" : hexa_code[i] = 10; break;
case "B" : hexa_code[i] = 11; break;
case "C" : hexa_code[i] = 12; break;
case "D" : hexa_code[i] = 13; break;
case "E" : hexa_code[i] = 14; break;
case "F" : hexa_code[i] = 15; break;
}
}
}
document.write(hexa_code);
}
hexa_to_dec(user_input);