When dividing by zero, the result is "Infinity". How to make it so that instead of Infinity, it was written in the window "You cannot divide zero!", Or did the alert pop up with this message? Here is the calculator code:

<input type="button" value=" &#247; " onclick="calculator.answer.value += '/';checkLength(this);checkDivToZero(this);" /> function checkDivToZero(input) //функція ділення { var value = input.value; if (input.value =='Infinity') { alert("Divide by zero error"); } } 
  • one
    Something I don’t see here is the division function - Herrgott
  • one
    I propose to make a function that accepts 2 agruments, numerator and denominator, and to check the denominator for equality to zero, if it is 0 - error output - Herrgott

1 answer 1

Try this

 function checkDivToZero(input) //функція ділення { var value = input.value; if (!isFinite(input.value)) { alert("Divide by zero error"); } } 
  • Infinity is one of the special values ​​for Number , besides you have a syntax error in the if (input.value typeof Infinity) line if (input.value typeof Infinity) so you can't write - Grundy
  • I repeat once again: Infinity is one of the special values ​​of Number - there is no such type, respectively, the if (typeof input.value == 'Infinity') condition if (typeof input.value == 'Infinity') will always return false - Grundy
  • Well, if you take a closer look - input in this context is a button with value=" &#247; " - Grundy
  • no-no !isFinite right. But here it still does not help because the number is not checked here - Grundy
  • This is the correct function if you transfer the result of the action to it. Oh, and for a long time I went to this ...) - alias