Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda