Javascript Brainteaser - Reverse Number Determining
<p>Lets say I have a list of numbers in the following form(Ignore the <code>|</code> they are there for formating help).</p>

<pre><code>00|00|xx
00|xx|00
xx|00|00
etc.
</code></pre>

<p><strong>Rules:</strong> XX can be any number between 1 and 50.  No XX values can be identical.</p>

<p>Now I select a random set of numbers(no duplicates) from a list qualifying the above format, and randomly add and subtract them.  For example </p>

<p><code>000011 - 002400 - 230000 =  -232389</code></p>

<p>How can I determine the original numbers and if they were added or subtracted solely from <strong>-232389</strong>?  I'm stumped.</p>

<p>Thanks!</p>

<p><strong>EDIT:</strong>
<s>I was looking for a function so I ended up having to make one.  Its just a proof of concept function so variables names are ugly <a href="http://jsfiddle.net/jPW8A/" rel="nofollow">http://jsfiddle.net/jPW8A/</a>.</s> </p>

<p>There are bugs in the following implementation, and it fails to work in a dozen of scenarios.  Check the selected answer below.</p>

<pre><code>function reverse_add_subtract(num){
    var nums = [];

    while(num != 0){
        var str = num.toString(),
            L = Math.abs(num).toString().length,
            MA = str.match(/^(-?[0-9]?[0-9])([0-9][0-9])([0-9][0-9])*$/);
        if(MA){
            var num1 = MA[1],
                num2 = MA[2];
        }else{
            var num1 = num,
                num2 = 0;
        }
        if(L%2)L++;
        if( num2 &gt; 50){
            if(num &lt; 0) num1--;
            else num1++;
        }
        nums.push(num1);
        var add = parseInt(num1 + Array(--L).join(0),10);
        num = (num-add);
    }
    return nums;
}
reverse_add_subtract(-122436);
</code></pre>
