Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
Struggling with Number.isInteger()

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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

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>

about 4 years ago · Juan Pablo Isaza Relatório

0

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda