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

265
Vistas
problem with a simple program (check a number is an even number or odd number using javascript)

I am writing a very simple program in JavaScript to check a number is an odd number or even number. and here is my code.

  function run(number) {
    var result;
    if(number % 2 == 0) {
      result == 'even';
    }
    else {
      result == 'odd';
    }
    return result;
  }

As you can see, very easy to understand, right? I have the variable number and check it. I thought that my algorithm is correct. But the program still does not run.

Could you please show me why the program has the problem ? Thank you very much for your time.

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

0

Two things here

function run(number) {
    var result;
    if (number % 2 == 0) 
        result = 'even';
    else 
        result = 'odd';
    
    return result;
}

const val = run(11);
console.log(val);

You should be using = (assignment operator) as you're assigning value to a variable and not comparing them (== or === are called comparison operators).

And make sure you actually call the function

about 4 years ago · Juan Pablo Isaza Denunciar

0

As the other answers & comments already point out, == is not storing the result but instead, an operator to compare two variables. One equal sign would do the job. Additionally, I'm suggesting the shorthand version (recommended once you understood what went wrong in the first place):

function run(number) {
  return (number % 2 == 0) ? 'even' : 'odd';
}
console.log(run(5))
console.log(run(6))

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