0

I am a beginner in JavaScript.

This code is supposed to evaluate a postfix expression. But it doesn't work. I think that the code is very logical and I don't really know what's wrong with it.

If someone could help me, I would be grateful!

<html>
<head>
</head>

<body>
<!-- starting javaScript -->
<script type = "text/javascript">
    var postStr = "123++"; //initial postfix expression     
    var postArr =  new Array();
    postArr = postStr.split(""); //string.split() turns string into array!

    for(var i=0; i<postArr.length; i++)
        document.write("value"+postArr[i]+"<br>");

    var stack=[];
    var result;
    var firstNum;
    var secNum;
    //var k;
    for(var i=0; i<postArr.length; i++)
    {
        if((postArr[i]!="^")||(postArr[i]!= "+")||(postArr[i]!= "-")||      (postArr[i]!= "*")||(postArr[i]!= "/"))
        {
            stack.push(postArr[i]);
            //document.write("length" + stack.length);
        }

        else if((postArr[i]=="^")||(postArr[i]== "+")||(postArr[i]== "-")||(postArr[i]== "*")||(postArr[i]== "/"))
        { 
            if(postArr[i]=='+')
            {
                firstNum=stack.pop();
                secNum=stack.pop();
                result = secNum + firstNum;
                stack.push("result = " + result);
            }
            else if(postArr[i]=='*')
            {
                firstNum=stack.pop();
                secNum=stack.pop();
                result = secNum * firstNum;
                stack.push("result = " + result);
            }
            else if(postArr[i]=='/')
            {
                firstNum=stack.pop();
                secNum=stack.pop();
                result = secNum / firstNum;
                stack.push("result = " + result);
            }
            else if(postArr[i]=='-')
            {
                firstNum=stack.pop();
                secNum=stack.pop();                                        
                                    result = secNum - firstNum;
                stack.push("result = " + result);
            } 
        }
    }
    var finalRes=stack.pop();
    document.write(finalRes); 
</script>
</body>
</html>
Joe
  • 38,368
  • 16
  • 103
  • 119
user1910524
  • 113
  • 3
  • 11

2 Answers2

3

There is some errors :

  1. your first condition should be separated by and (&&) operators instead of or since you are using negative condition

  2. since you convert string to numbers don't forget to convert the characters into integers (with something like +postArr[i])

  3. you should push the result and not a string with a comment (stack.push(result) and not stack.push("result = " + result)).

See this jsfiddle : http://jsfiddle.net/scaillerie/GwDTM/2/.

Samuel Caillerie
  • 8,032
  • 1
  • 24
  • 31
  • @Shlomi Fish, with this condition: if((postArr[i]!="^")||(postArr[i]!= "+")||(postArr[i]!= "-")|| (postArr[i]!= "*")|| postArr[i]!= "/")) , I don't want any operator to enter the loop, so, I want the element entering to be a number. If you see any other errors, please notify me. – user1910524 Dec 17 '12 at 16:53
  • I understand number 1 and number 3, but I don't quite understand number 2. Could you please clarify? – user1910524 Dec 17 '12 at 16:56
  • @bfavaretto : Sorry yes... I have updated my post. @user1910524 : For point 2, if you keep `postArr[i]` as it is, you will push characters and when you will do some addition, you will add a concatenation (`'1' + '2' = '12'` whereas you want `1 + 2 = 3`). – Samuel Caillerie Dec 17 '12 at 16:59
  • @user1910524 : I have updated my fiddle so that you can test if it corresponds to what you want! – Samuel Caillerie Dec 17 '12 at 17:00
  • @user1910524: He wants you to convert the strings (characters) to numbers, else `"1"+"2"=="12"` not `3`. This can be done by `Number(str)`, `parseInt(str, 10)` or by using the [unary plus operator](http://stackoverflow.com/q/5450076/1048572) – Bergi Dec 17 '12 at 17:01
  • @ Samuel Caillerie, does just putting a "+" in front of the array name convert the value to a number? – user1910524 Dec 17 '12 at 17:04
  • @ Bergi, do you mean by "using the unary plus operator" as Mr. Samuel Caillerie said above? To write it like this?: +postArr[i] – user1910524 Dec 17 '12 at 17:06
  • @user1910524 : yes putting a `+` in front of a character (not an array, but in the code, I have specified an index so that it applies to a character), is an efficient way to convert it into a number... But the "safest" way, is `parseInt(str, 10)` as stated by Bergi but a little less efficient... – Samuel Caillerie Dec 17 '12 at 17:09
  • Thank you so much @Samuel Caillerie – user1910524 Dec 17 '12 at 17:39
  • @ Samuel Caillerie, when I change postStr to "541+-" for example, it doesn't output correctly! – user1910524 Dec 17 '12 at 17:45
  • @user1910524: when I enter "541+-" in the input box and I submit, I got the correct answer (0)... – Samuel Caillerie Dec 17 '12 at 17:49
1

I'm not sure if it's the only problem but the condition this code:

if((postArr[i]!="^")||(postArr[i]!= "+")||(postArr[i]!= "-")|| (postArr[i]!= "*")|| postArr[i]!= "/"))

Will always evaulate to true because postArr[i] will always not be one of the options. You probably want something like:

var ch = postArr[i];

if (! ((ch == "^") || (ch == "+") || (ch == "-")…

Shlomi Fish
  • 4,070
  • 2
  • 21
  • 27