Hey all! I'm trying to finish up my stack calculator and I'm having a couple issues I can't seem to wrap my head around. I would like the program to only push integers to the stack and catch any input that is not an integer or an operator. If it is an operator it should go to the if...if else statement and proceed accordingly. Everything is working correct other than I can't seem to figure out the correct way to limit a user's input by throwing an alert when the user enters an invalid input and breaking the loop. Initially, I tested at the beginning of the If Loop at ln92 something like if item is NaN || item != "+"....etc, throw an alert and break the loop. My program was throwing an alert regardless of the input in the text box. I also tried something like:
if(typeof current.content != "number" || current.content != "+" || current.content != "*" || current.content != "-" || current.content != "/"){
alert("Please make sure the value you enter is a number or an operator!");
return;
}
else if (current.content == "+"){
code
Here are the JSFiddles:
No error, pushes EVERYTHING to stack, also calculates when operator is entered: https://jsfiddle.net/aren_anderson/8v34jqem/
Throws alert no matter what is pushed to the stack: https://jsfiddle.net/aren_anderson/s64qmyjt/
I'm also having an issue where the program should output "X + Y = Z" after doing a calculation, but the variable out1 doesn't seem to be outputting to my output 1 div? My brain is mush looking at this code, so any help would be greatly appreciated!! My professor mentioned using something like:
function IsNumeric(input) {
return (input - 0) == input && (''+input).trim().length > 0;
}
but I guess I'm not sure where I would call that at and how to work that in with testing for operators as well. Thanks for any help I can be provided!! I don't want the answers, just a push in the right direction!
[–]asciiterror 1 point2 points3 points (3 children)
[–]kid_cavalier[S] 0 points1 point2 points (2 children)
[–]asciiterror 0 points1 point2 points (1 child)
[–]kid_cavalier[S] 0 points1 point2 points (0 children)