Justin
Justin

Reputation: 577

Using javascript to decode a string

I am doing a challenge on hacker.org, this is what it says "I have a special piece of text that represents a specific value. Here's how I calculate it: Start reading left to right, If a character is a digit, I add it to my sum, If it's an 'x', I remove it and go left two places, Continue until I hit the end of the string. For example, the string '123x456' has a value of 26.

What is the value of '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'?"

This is the code that I wrote to solve this task

    function decrypt(text){
        var sum = 0; 
        for(var i = 0; i < text.length ; i++){
            if(isNaN(parseInt(text[i])) == false){
                sum += parseInt(text[i]);
            }
            else if(text[i] == "x" || "X"){
                text.replace(text[i],0);
                i = i - 2;

            }
        }
        return sum;
    }
document.write(decrypt('93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'));

When I run this code it seems to run an infinite loop, why is this?

Upvotes: 0

Views: 462

Answers (2)

KooiInc
KooiInc

Reputation: 122888

You can resolve the problem of string immutability (as per answer of Fike) by converting it to an array before you start your operations on it. Your function can be written as:

function decrypt(str){
    var s = str.split('') //=> convert to Array
       ,i = -1
       ,sum = 0;
    while ((i+=1)<s.length){
        sum += +s[i] || 0;
        if (/[xX]/.test(s[i])) {
            s[i] = 0;
            i -= 3;
        }
    }
    return sum;
}
// decrypt('123x456') => 26

Upvotes: 0

Etki
Etki

Reputation: 2144

.replace() method doesn't alter original string (strings are immutable in js), it just returns new string with replaced substring, so replacing 'X' or 'x' with zero should look a little different:

text = text.replace(text[i], 0);

Please note that this code will work fine in your function, but it doesn't regard the exact position of replaced substring (it finds first occurrence and replaces it). In other cases you might consider using .substr() to replace particular character as following:

// concatenating part before character, '0' and part after character
text = text.substr(0, i) + '0' + text.substr(i + 1);

One more problem not mentioned above:

else if(text[i] == "x" || "X")

There are two statements in the if structure: text[i] == "x" and "X". While first one is perfectly fine, the second one will always evaluate to true, shifting i backwards by two every time this statement is reached - sadly, JS can't guess if you're trying to coerce "X" to boolean or want to know if text[i] equals "X", it just evaluates every expression as single one, and "X" is casted to true.

Upvotes: 1

Related Questions