I'm trying to make a calculator with Javascript. I have a function that asks to input an operator. I want it to ask for an operator again if the user enters anything else than *, /, + or -. Everything works fine if the user enters one of the accepted operators, however, if the wrong operator has been entered, and the function is executed for a second time, the value of the operator is somehow "undefined". And I am not sure why... When I call the function within the if statement, does it not jump back to the beginning of getOperator(), where I assign a value to the operator variable? Can anyone tell me what I've done wrong?
function getOperator() {
var operator = getStringInputWithPrompt('Please enter the operator:');
if ((operator !== "+") && (operator !== "*") && (operator !== "-") && (operator !== "/")) {
console.log("\nSorry that was not a valid operator");
getOperator();
} else {
return operator;
}
}