View Single Post
 12-13-2012, 12:04 AM PM User | #10 devnull69 Senior Coder   Join Date: Dec 2010 Posts: 2,355 Thanks: 11 Thanked 558 Times in 551 Posts As this is supposed to be your exercise, I can only give you more hints 1 - Let's hope that you don't have to take care of operator priority (so you can just go right to left) 2 - call your function with the current string 3 - Start from the right of the current string, extract the number and the operator and use it for the expression like "number operator yourfunction(restofstring)". If there is no operator you end the recursion and return the number Example: 14x10+2x1 step 2: alert(yourfunction("14x10+2x1")); step 3: inside of yourfunction: extract 1 and x, return 1*yourfunction("14x10+2") step 3: inside of yourfunction: extract 2 and +, return 2+yourfunction("14x10") step 3: inside of yourfunction: extract 10 and x, return 10*yourfunction("14") step 3: inside of yourfunction: just return 14 => end of recursion one level up: return 10*14 = 140 => end of recursion one level up: return 2+140 = 142 => end of recursion one level up: return 1*142 = 142 => end of recursion one level up: alert(142) => result
 Users who have thanked devnull69 for this post: 123jo (12-13-2012)