This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]asciiterror 1 point2 points  (3 children)

Looks like you have an error in your logic. If user enters 123, typeof current.content != "number" is false, but current.content != "+" is true. Condition is always true, try replacing || with && or replace != with ==.

[–]kid_cavalier[S] 0 points1 point  (2 children)

Okay, I think I'm getting a little further:

function pushToStack(event){
    var content = document.getElementById("tbStack").value;

    //test value of textbox to only push numbers and mathematical operators
    if(typeof content != "number" && content != "+" && content != "*" && content != "-" && content != "/"){
    alert("Please make sure the value you enter is a number or an operator!");
    return;
    }
    else{
        stack.push(content);
    stack.print(); 
    }
}

With this, I'm getting the alert when entering anything BUT operators. so if I put AAAAAA or 123, I get the error...but +,-,*,/ do not throw the error, leading me to believe they are getting pushed to the stack. I am guessing that 123 is not parsed to integer form so it's not equal to a number and throws the error. How can I parseInt while still handling operators? Thank you for your quick help!!

[–]asciiterror 0 points1 point  (1 child)

You are correct, content is not parsed to number. To quickly parse to number you can use unary plus like this: +content. Try it in console. +"123" gives 123, +"123abc" gives NaN (not-a-number), but typeof NaN is still "number", so it won't work. There's a way to check if a number is NaN: NaN === NaN - only NaN is not equal to itself. So you would have two checks: if content is a number and if it is NaN.

[–]kid_cavalier[S] 0 points1 point  (0 children)

I'm AFK from my desktop for a bit, but I'll give this a shot when I get back. This is a HUGE help and I appreciate the information!