'Postfix Algorithm Evaluation in JS
this is what we start with: '7.7+7'
The input of the function is an array of strings (original input converted to postfix): 7.7,7,+
then would get fed back into another function in order to display within my html page. this function is nested in another parent function to call with class object.
this.postfixEval = function(postfixArray){
var stack = new Stack();
for( element of postfixArray){
console.log("element: " + element);
if(isNaN(element)){
var x = stack.pop();
var y = stack.pop();
console.log("var x/y: " + x + " " + y + " element: " + element) ;
if (element == "+"){
result = (y+x);
console.log("Expected Result: " + result)
stack.push(y + x);
} else if (element == '-'){
stack.push(y - x);
} else if (element == '*'){
stack.push(y * x);
} else if (element == '/'){
stack.push(y / x);
}
} else {
stack.push( parseFloat(element) );
}
}
//final check for non numbers within the stack
var returnValue = null;
while( !stack.isEmpty() ){
stack.print();
var element = stack.pop();
if(isNaN(element)){
continue;
} else{
returnValue = element;
}
}
return returnValue;
}
I'm not sure what i'm doing wrong? the output is simply 7.7. Here is a sample of the logging in attempt to debug:
scripts.js:75 postFix: 7.7,7,+
scripts.js:145 undefined
scripts.js:175 element: 7.7
scripts.js:175 element: 7
scripts.js:175 element: +
scripts.js:180 var x/y: 7 7.7 element: +
scripts.js:76 result: null
Thank you for your time and help in advance.
Solution 1:[1]
If I replace your new Stack
with a simple array, and make the necessary adjustments to accommodate that change, I get the correct answer...
function postfixEval( postfixArray ) {
var stack = [];
for( element of postfixArray){
console.log("element: " + element);
if(isNaN(element)){
var x = stack.pop();
var y = stack.pop();
console.log("var x/y: " + x + " " + y + " element: " + element) ;
if (element == "+"){
result = (y+x);
console.log("Expected Result: " + result)
stack.push(y + x);
} else if (element == '-'){
stack.push(y - x);
} else if (element == '*'){
stack.push(y * x);
} else if (element == '/'){
stack.push(y / x);
}
} else {
stack.push( parseFloat(element) );
}
}
//final check for non numbers within the stack
var returnValue = null;
while( stack.length > 0 ){
console.log( stack );
var element = stack.pop();
if(isNaN(element)){
continue;
} else{
returnValue = element;
}
}
return returnValue;
}
postfixEval(['7.7','7','+']);
Solution 2:[2]
This is my approach:
const value = '2 3 * 1 - 5 /';
function postfixEval( expr ) {
let stack = [];
const arr = expr.split('').filter((el)=>el !== " " )
console.log( arr );
for( element of arr){
console.log("element: " + element);
if(isNaN(element)){
var y = stack.pop();
var x = stack.pop();
const result = eval(x+element+y)
stack.push(result)
console.log( result );
} else {
stack.push( parseFloat(element) );
}
}
console.log( typeof stack[0] );
return stack[0];
}
Solution 3:[3]
Lee SEE My Approach
function evaluatePostfix(exp) {
let stack=[];
for(let i=0;i<exp.length;i++)
{
let c=exp[i];
if(! isNaN( parseInt(c) ))
stack.push(c.charCodeAt(0) - '0'.charCodeAt(0));
else
{
let val1 = stack.pop();
let val2 = stack.pop();
switch(c)
{
case '+':
stack.push(val2+val1);
break;
case '-':
stack.push(val2- val1);
break;
case '/':
stack.push(val2/val1);
break;
case '*':
stack.push(val2*val1);
break;
}
}
}
return stack.pop();
}
let exp="121*+2-"; document.write("postfix evaluation: "+evaluatePostfix(exp));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Trentium |
Solution 2 | Mansour Ashraf |
Solution 3 | iamKaranSharmaB |