-1

I have this code and I'm trying to add some IF statements to the submit button, so it does not submit anything empty, or other than numbers and arithmetic operators, or shows error when operands are more than operators and vice versa, however it doesn't seem to work. Here is the full code.

const handleSubmit = (e) => {
  if (inputValue.length === 0) {
    e.preventDefault();
    setResult("You did not enter anything!")
  }
  if (inputValue === isNaN && inputValue !== validOperators) {
    e.preventDefault();
    setResult("only numbers!")
  } else {
    e.preventDefault();
    setResult(solution);
    setInputValue("");
  }
}
isherwood
  • 58,414
  • 16
  • 114
  • 157
Louyi
  • 69
  • 8

1 Answers1

1

isNan is not a value, it's a function. You need to call it.

I made the assumption that validOperators is an array or a string containing the valid operators.

If so, this is what you want:

const handleSubmit = (e) => {
if (inputValue.length === 0) {
  e.preventDefault();
  setResult("You did not enter anything!")
} if (isNaN(inputValue) || !validOperators.includes(inputValue)) {
  e.preventDefault();
  setResult("only numbers (and valid operators)!")
} else {
  e.preventDefault();
  setResult(solution);
  setInputValue("");
 }
}
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116