Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda