0

I'm trying to write a recursive function that, if it contains "expr" as a key instead of "number" it will feed that section of the JSON back into the function. My code is the following:

var result = 0;

function calc(str) {
var obj = JSON.parse(str);
    if(obj.hasOwnProperty("expr")) {
        var temp = obj.expr;
        JSON.stringify(temp);
        calc(temp);
    }
    if (obj.op == "add") {
        var i = parseInt(obj.number);
        result += i;    
    } else if(obj.op == "subtract") {
        var i = parseInt(obj.number);
        result -= i;
    }
    return result;
}

I'm getting a Syntax Error saying "Unexpected token o in JSON at position 1." Does Stringify not reformat it back into a string? If so, how would I go about that?

My input is something like the following:

result = calc('{"op" : "add", "number" : 5}');
result = calc('{"op" : "subtract", "number" : 2}');
result = calc('{"op" : "add", "number" : 19}');
result = calc('{"op": "subtract", "expr" : {"op" : "add", "number" : 15}}');

6
  • 2
    can you please share the json Commented Oct 24, 2017 at 4:23
  • 1
    JSON.stringify returns a string, it doesn't modify its argument. The mistake is the line above calc(temp) Commented Oct 24, 2017 at 4:27
  • look at my answer Commented Oct 24, 2017 at 4:39
  • Not: You are not converting a "JSON object" to a string. You are converting a JavaScript object to JSON. Commented Oct 24, 2017 at 4:40
  • 1
    @jackarms "it doesn't modify its argument." Note, JSON.stringify() can modify the argument passed to return a different result than argument as a string Commented Oct 24, 2017 at 4:47

2 Answers 2

1

Should be like this:

function calc(str) {

    var res = 0;
    var obj = (typeof str =="string") ? JSON.parse(str) : str;

    if(obj.hasOwnProperty("expr")) obj.number = calc(obj.expr);

    switch(obj.op){
      case "add":   
          res += +obj.number;
      break;
      case "subtract":
          res -= +obj.number;
      break;
    }    
    return res;
}

Input

var result = 0;
result += calc('{"op" : "add", "number" : 5}'); 
result += calc('{"op" : "subtract", "number" : 2}'); 
result += calc('{"op" : "add", "number" : 19}'); 
result += calc('{"op": "subtract", "expr" : {"op" : "add", "number" : 15}}');
Sign up to request clarification or add additional context in comments.

Comments

0

I got it figured out.

var result = 0;

function calc(str) {
    var obj = JSON.parse(str);
    var i = parseInt(obj.number);
    if(obj.hasOwnProperty("expr")) {
        var temp = obj.expr;
        temp = JSON.stringify(temp);
        var i = calc(temp);
    }     
    if (obj.op == "add") {
        result += i;
    } else if(obj.op == "subtract") {
        result -= i;
    }
    return result;
}

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.