1

I have an assignment to make a converter from decimal to binary numbers and vice versa. I have done everything I need, but I have no idea how to protect the site from the user entering a number with the part after the decimal point.

So I fooled around the internet and found Number.isInteger(), yet I have no idea how this works. Tried writing some test stuff (as you can observe beneath this wall of text) but nothing seemed to work. Can anyone offer any help as to how exactly Number.isInteger() works?

pepege() {
  var a = +document.getElementById("x").value;
  var b = Number.isInteger(a);
  document.getElementById("out").innerHTML = b;
}
<input type="text" id="x">
<br>
<input type="button" value="xddddd" onclick="pepege();">
<br>
<a id="out"></a>

Thanks for any and all advice on how to move forward with this.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • You can test most JS code on the CLI using Node.JS. Write a file `file.js` that you want to execute and run `node file.js` to see the output. It does not work with the DOM though, but you don't need the DOM to figure out what most JS functions do. – libby Dec 15 '21 at 21:23
  • 1
    Are you asking how the method works (behind the scenes), or are you asking how to _implement_ it for your case? Please see [ask] and take the [tour]. – isherwood Dec 15 '21 at 21:25
  • 1
    Add `function` before `pepege() {`, then your code will work and display true/false as expected. But I think testing how `Number.isInteger` (or any other function, for that matter) works would be much easier in the devtools console... – CherryDT Dec 15 '21 at 21:25
  • Do a google search for Number.isInteger(), which returns a boolean value. As this is homework I didn't want to give you an precise code answer – Phaelax z Dec 15 '21 at 21:26
  • @Leau, please do not make edits that correct code in questions. That masks problems that need to be addressed. – isherwood Dec 15 '21 at 21:27
  • 1
    *What* doesn't work? What do you expect to happen and what happens instead? – VLAZ Dec 15 '21 at 21:31

2 Answers2

0

You can add event listener and force user to enter just number. As i understand you are having problem with user inputs

const pepege = () => {
  var a = +document.getElementById("x").value;
  var b = Number.isInteger(a);
  document.getElementById("out").innerHTML = b;
}

 
document.getElementById("x").addEventListener("input", event => {
  document.getElementById("x").value = document.getElementById("x").value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1')
});
<input type="text" id="x">
<br>
<input type="button" value="xddddd" onclick="pepege();">
<br>
<a id="out"></a>
Evren
  • 4,147
  • 1
  • 9
  • 16
-1

Wrap your var a in parseFloat(). Input type text always returns a string. With parseFloat(), you'll get NaN for non-numeric characters, a float for a float, and an integer for an integer.

function pepege() {
  var a = parseFloat(document.getElementById("x").value);
  var b = Number.isInteger(a);
  document.getElementById("out").innerHTML = b;
}
<input type="text" id="x">
<br>
<input type="button" value="xddddd" onclick="pepege();">
<br>
<a id="out"></a>
symlink
  • 11,984
  • 7
  • 29
  • 50
  • It's already using [a unary plus that converts to a number](https://stackoverflow.com/questions/15129137/what-does-mean-in-javascript). There is no reason to use `parseFloat` unless you believe the input makes a difference. Yet, OP hasn't shared what input they try. – VLAZ Dec 15 '21 at 21:29
  • Add the unary plus and it returns `true` if you enter a number like `1`. My comment is correct. – VLAZ Dec 15 '21 at 21:31
  • @VLAZ No, you're not correct, because the unary plus returns true with floats. – symlink Dec 15 '21 at 21:32
  • [feel free to try it](https://jsbin.com/hukixik/1/edit?html,js,output) and enter `1` and press the button then `1.2` and press the button. You'd see the values "true" then "false". – VLAZ Dec 15 '21 at 21:34
  • @VLAZ weird, it wasn't working for me before. – symlink Dec 15 '21 at 21:36