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

185
Vistas
Decide the smallest even number from 2 input , correct it if the input number is not even. Any solution(Javascript/html)?

the code always thinks num1 is the smaller number ,even if it's not + if the second input is odd, but the first is even, then the output is num1 the smaller even instead of "second input is odd, correct it"

+I would also need a "Both numbers are the same, correct it" output

this would be my wrong solution, how could I make it work?

<body>
    <input id="num1">
    <input id="num2">
    <button onclick="myFunction()">Calculate</button>
    <p id="p"></p>
</body>
const p = document.getElementById("p")
function myFunction() {
    var num1, num2;
    num1 = Number(document.getElementById("num1").value);
    num2 = Number(document.getElementById("num2").value);

     if(num1%2==0 >num2%2==0){
        p.innerHTML=num2+" is the smaller even number"
        if(num1%2!=0){
            p.innerHTML=num1+" first input is odd, correct it"
        }
        else {
        p.innerHTML=num2+" second input is odd, correct it"
        }
     }
     else{
        p.innerHTML=num1+" is the smaller even number"
     }


}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You are comparing boolean values in this if statement:

if(num1%2==0 >num2%2==0)

Change that to

if(num1 > num2)
about 4 years ago · Juan Pablo Isaza Denunciar

0

In your version, you don't address the possibility that both are odd and you are also comparing two booleans.

function myFunction() {
  var num1, num2;
  num1 = Number(document.getElementById("num1").value);
  num2 = Number(document.getElementById("num2").value);
  if(num1%2!==0 && num2%2!==0){
    p2.innerHTML = num1 + 'and' + num2 + 'are odd, correct them';
  }
  else if(num1%2!==0 && num2%2===0){
    p2.innerHTML = num1 + 'is odd, correct it';
  }
  else if(num1%2===0 && num2%2!==0){
    p2.innerHTML = num2 + 'is odd, correct it';
  }
  else{
    if(num1 < num2){
        p2.innerHTML=num1+" is the smaller even number"
    }
    else if(num2 < num1) {
      p2.innerHTML=num2+" is the smaller even number";
    }
    else {
      p2.innerHTML = 'They are the same number, correct one'
    }
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is some updated code. I am not sure what your source code looks like so I'm basing it off my own minimum reproducible example.

I figured it would be easier to read if you split it up first by checking if both numbers are even. If they are not, check out which one is not even.

If they are even, you can just print the smaller one. I did it at the beginning for convenience but you can move it into the else statement if that's preferred.

function myFunction() {
    var num11, num2;
    var p = document.getElementsByTagName("P")[0]
    num1 = Number(document.getElementById("num1").value);
    num2 = Number(document.getElementById("num2").value);
    
    var smaller = num1 < num2 ? num1 : num2

     if(num1%2!=0 || num2%2!=0){
        if(num1%2!=0){
            p.innerHTML=num1+" first input is not even, correct it"
        } else {
        p.innerHTML=num2+" second input is not even, correct it"
        }
     }
     else if (num1 === num2){
        p.innerHTML="Both numbers are equal, correct it."
     } else {
        p.innerHTML=smaller+" is the smaller even number"
     }


}
<p></p>

<input id="num1"/>
<input id="num2"/>

<button onclick="myFunction()">Run</button>

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