Search code examples
javascripthtmlalgorithmpostfix-mtapostfix-notation

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

  • 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','+']);