JSFiddle - React, Tailwind, and code Playground

by Torsten Walter

HTML

<div id='output'>

</div>

JavaScript

function calculate (expression) {
  // return early for invalid inputs
  if (!expression) return 0;
  
  const stack = [];
  expression = expression.split(" ");
  let op;
  
  const ops = {
  	'+': (a, b) => a + b,
    '-': 
  };
  
  while (op = expression.shift()) {
    console.log(op)
    if (op == +op) {
      // is numerical
      stack.push(op);
    } else {
      // perform operation
      const right = stack.pop();
      const left = stack.pop();
      stack.push(ops(op)());
      
    }
  }
  return stack.pop();
}

function add(left, right) {}

const result = calculate('3 2 - 5 2 ')
document.querySelector('#output').innerText = result