Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
How can I loop through an object (for a simple math interpreter, in Javascript)

I've been working on a simple math interpreter and I'm stuck on a problem.
I can't figure out how to loop through the object in the interpreter. My attemps either don't work at all, or cause infinite loops until Javascript reaches it's max amount of memory.

The result of the Parser would look something like this for a simple 2 + 2 + 3:

{
    "operator": "+",
    "left": {
        "operator": "+",     
        "left": {
            "type": "NUMBER",
            "value": 2       
        },
        "right": {
            "type": "NUMBER",
            "value": 2       
        }
    },
    "right": {
        "type": "NUMBER",    
        "value": 3
    }
}

Here's one of the attempts I've made.

interpret(node) {
    node.left = this.parseNode(node.left);
    node.right = this.parseNode(node.right);

    return this.parseNum(node.left, node.right, node.operator);
}

parseNode(node) {
    let left = node.left;
    let right = node.right;

    while (left != null) {
        left = this.destructure(left);
    }
    while (right != null) {
        right = this.destructure(right);
    }

    if (left == null && right == null) {
        return { ...node };
    } else {
        return {
            type: "NUMBER",
            value: this.parseNum(left, right, node.operator),
        };
    }
}

The parse number (parseNum) function is pretty simple, so I don't think I need to share it. All it does is take the operator and add/multiply/subtract/divide the first two items based on that.

Any help would be appreciated, thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

What you want is to recursively reduce each node into a single value by inspecting it and...

  • If the node is already a value node, just return it
  • Otherwise, create a new value node by reducing both the left and right nodes to a single value node and evaluating the result with the operator

const root = {"operator":"+","left":{"operator":"+","left":{"type":"NUMBER","value":2},"right":{"type":"NUMBER","value":2}},"right":{"type":"NUMBER","value":3}}

// Operator functions
const operators = {
  "+": (l, r) => l + r,
}

// Expression evaluation
const evaluate = ({ value: l }, { value: r }, operator) =>
  operators[operator](l, r)
  
const isValueNode = node => "value" in node
  
// Reduce a node to a _value_ node
const reducer = (node) => {
  // Already a value node? Just return it
  if (isValueNode(node)) return node
  
  return {
    type: "NUMBER", // no idea what this is for ¯\_(ツ)_/¯
    value: evaluate(
      reducer(node.left),  // recursively reduce the _left_ node
      reducer(node.right), // recursively reduce the _right_ node
      node.operator
    )
  }
}

console.log(reducer(root))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!