Skip to content

Instantly share code, notes, and snippets.

@kermit-klein
Created August 24, 2021 12:31
Show Gist options
  • Save kermit-klein/8525142fb1b96a490644f3585803ede2 to your computer and use it in GitHub Desktop.
Save kermit-klein/8525142fb1b96a490644f3585803ede2 to your computer and use it in GitHub Desktop.
function calculate(obj) {
switch (obj.type) {
case "number":
return parseInt(obj.value);
case "+":
return calculate(obj.left) + calculate(obj.right);
case "-":
return calculate(obj.left) - calculate(obj.right);
case "*":
return calculate(obj.left) * calculate(obj.right);
case "/":
return calculate(obj.left) / calculate(obj.right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment