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
I wonder why this simple javascript code is not running

I am facing a problem while want to, output=pick "Positive","Negative","Zero" as message if number(user puts in input box) is + or - or 0, respectively.

I am writing code- but unable that Zero is coming in message everytime, whether putting anything.

I am writing this

var n1 = Number(number.value)
var finalresult;
if (n1 > 0) {
  finalresult = `Positive`
} else if (n1 == 0) {
  finalresult = `Neutral`
} else {
  finalresult = `Negative`
}

function know() {
  document.getElementById("result").innerText = finalresult
}
<input type="text" id="number"></input>
<button id="btn" onclick=know()>Know</button>
<p id="result"></p>

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

0

you have to recover the value of number input after it was updated

after a click on button or change event on input

    function know(){
        var n1=parseInt(document.getElementById('number').value);
        var finalresult;
        if(n1>0)
        {finalresult=`Positive`}
        else if(n1==0)
        {finalresult=`Neutral`}
        else
        {finalresult=`Negative`}
      document.getElementById("result").innerText=finalresult
    }
<input type="text" id="number" ></input>
<button id="btn" onclick=know()>Know</button>
<p id="result"></p>

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to put the JavaScript code into the know() function, so that the code is ran everytime the button is clicked and the know() function is triggered. In your question, finalresult is set when the input has no value (an empty string) and as '' is cast to 0 when using ==, '' == 0 returns true.

<input type="text" id="number" />
<button id="btn" onclick="know()">Know</button>
<p id="result"></p>
<script>
  function know() {
    var number = +document.getElementById("number").value;
    var finalresult;
    if (number > 0) {
      finalresult = `Positive`;
    } else if (number == 0) {
      finalresult = `Neutral`;
    } else {
      finalresult = `Negative`;
    }
    document.getElementById("result").innerText = finalresult;
  }
</script>

P.S. You could always consider using === instead of == ('' === 0 returns false).

about 4 years ago · Juan Pablo Isaza Relatório

0

function know() {
  var n1 = +document.getElementById("number").value;
  var finalresult;
  if (n1 > 0) {
    finalresult = `Positive`
  } else if (n1 == 0) {
    finalresult = `Neutral`
  } else {`enter code here`
    finalresult = `Negative`
  }
  document.getElementById("result").innerText = finalresult;
}
<input type="text" id="number"></input>
<button id="btn" onclick=know()>Know</button>
<p id="result"></p>

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