0

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);

2 Answers 2

2

Seems like there should be an easier way to do that, for instance just writing the result of the string manipulation back to the same variable

var user_input = prompt("Please enter an hexadecimal color code:","e.g. A876FF");

user_input = user_input.replace(/[ABCDEF]/g, function(x) {
    return {A:10,B:11,C:12,D:13,E:14,F:15}[x];
});

document.body.innerHTML = user_input;

FIDDLE

Sign up to request clarification or add additional context in comments.

2 Comments

Replacing the return statement with this return x.toUpperCase().charCodeAt(0) - 55; will add more obscurity.
@Amberlamps - I like that, and think it's pretty clever.
0

Because hexa_code is a string and is immutable, you cannot write back to it, like when you try hexa_code[i] = 10 in your switch statement.

Make a new variable to store your result.

Here, see the working code below (also at http://jsfiddle.net/legolandbridge/pn1t4jz1/)

var user_input = prompt("Please enter an hexadecimal color 

    code:","e.g. A876FF");

    var hexa_to_dec = function(hexa_code) {

            var output = "";

            for(var i = 0, limit=hexa_code.length; i < limit; i++) {

                    switch(hexa_code[i]){ 
                        case "A" : output += 10; break;
                        case "B" : output += 11; break;
                        case "C" : output += 12; break;
                        case "D" : output += 13; break;
                        case "E" : output += 14; break;
                        case "F" : output += 15; break;
                        default: output += hexa_code[i];
                        }

                    }

            console.log(output);
            };

    hexa_to_dec(user_input);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.