Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number.IsInteger() in if else statement not working

function bereken() {
  input1 = document.getElementById('input1').value;
  input2 = document.getElementById('input2').value;
  input1Valid = Number.isInteger(document.getElementById('input1'));
  input2Valid = Number.isInteger(document.getElementById('input2'));

  if (Number.isInteger(input1) && Number.isInteger(input2)) {
     alert('De getallen zijn allebei hele getallen')
  }
}
<!DOCTYPE html>
<html>
  <head>
    <script src='code.js'></script>
  </head>
  <body>
    <center>
      <h1>GGD calculator</h1>
      <p>Reken de Grote Gemene Deler uit van <input type='number' id='input1'> en <input type='number' id='input2'>.</p>
      <br>
      <input type='button' value='Bereken!' onClick='bereken()'>
    </center>
  </body>
</html>

I was practising JavaScript and I came across this issue, can anybody help me?

I want it to alert my message when both numbers entered are integers, why is this not working?

like image 289
Jojo Avatar asked Nov 09 '17 20:11

Jojo


People also ask

How do you check if a number is integer in JS?

The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .

How do you check if it is a number in JavaScript?

In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.

How do you check if a character is a number in JavaScript?

To check if a character is a number, pass the character as a parameter to the isNaN() function. The function checks if the provided value is NaN (not a number). If the function returns false , then the character is a valid number. Copied!

How do you check if a number is a float in JavaScript?

Check if the value has a type of number and is not an integer. Check if the value is not NaN . If a value is a number, is not NaN and is not an integer, then it's a float.

What is the use of isinteger in Java?

Definition and Usage. The Number.isInteger() method determines whether a value an integer. This method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise it returns false.

What is the return type of isinteger?

This method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise it returns false. Number.isInteger () is an ES6 feature (JavaScript 2015).

What is number number isinteger?

Number.isInteger () is an ES6 feature (JavaScript 2015). It is supported in all modern browsers: Number.isInteger () is not supported in Internet Explorer 11 (or earlier). Required. The value to be tested A Boolean.

How do you check if a number is integer in JavaScript?

JavaScript Number isInteger() Method ... The Number.isInteger() method determines whether a value an integer. This method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise it returns false.


1 Answers

The value of the #num element is returned as a string as you can see in the console. Just revert it to a number using + sign.

function bereken() {
  input1 = document.getElementById('input1').value;
  input2 = document.getElementById('input2').value;
  
  if (Number.isInteger(+input1) && Number.isInteger(+input2)) {
     alert('De getallen zijn allebei hele getallen')
  }
}
<!DOCTYPE html>
<html>
  <head>
    <script src='code.js'></script>
  </head>
  <body>
    <center>
      <h1>GGD calculator</h1>
      <p>Reken de Grote Gemene Deler uit van <input type='number' id='input1'> en <input type='number' id='input2'>.</p>
      <br>
      <input type='button' value='Bereken!' onClick='bereken()'>
    </center>
  </body>
</html>
like image 147
Sanchit Patiyal Avatar answered Oct 17 '22 02:10

Sanchit Patiyal