January 2018
Intermediate to advanced
332 pages
7h 36m
English
From here on, executing this postfix notation is fairly easy. The algorithm is relatively straightforward; you pop out each of the operators onto a final result stack. If the operator is one of *, ^, +, -, /, then evaluate it accordingly; otherwise, keep appending it to the output string:
function evaluate(postfix) { var resultStack = new Stack(); postfix = clean(postfix.trim().split(" ")); postfix.forEach(function (op) { if(!isNaN(parseFloat(op))) { resultStack.push(op); } else { var val1 = resultStack.pop(); var val2 = resultStack.pop(); var parseMethodA = getParseMethod(val1); var parseMethodB = getParseMethod(val2); if(op === "+") { resultStack.push(parseMethodA(val1) + parseMethodB(val2)); } else if(op ...
Read now
Unlock full access