View Single Post
Old 12-12-2012, 08:02 PM   PM User | #5
123jo
New Coder

 
Join Date: Dec 2012
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
123jo is an unknown quantity at this point
Maybe I wasn't clear enough... I learned how to go through my table by using recursivity so I wanted to apply the same method for a string, but I'm still stuck.

s is the string (for example "14x10+2x1"). The function str is then supposed to return 142.

Code:
var str = function(s){
    
    var i = 0;
 
    var helper = function(s) {
        
        if(i < s.length) {
            
            var t = s.substring(i, s.length)
            var stringNum = parseInt(t, 10);
            if( t.charAt(i) == "x" ) stringNum *= t.charAt(i+1);
            if( t.charAt(i) == "+" ) stringNum += t.charAt(i+1);
            
            i++;
        }
        return stringNum;
    }
    return helper(s);
};
I don't know where I made a mistake... it only returns 14. How can I do to make the function read "x" and "10" and the rest of the string ? Thanks
123jo is offline   Reply With Quote