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?
The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .
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”.
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!
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.
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.
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).
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With