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

129
Vistas
Multiple and different conditions in Javascript

I have a problem with logic. If the number is higher than 0 and less than 10 AND the number is 11 AND the number is 22 do something. (1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22).

And ELSE IF the number is higher than 10 BUT is NOT 11 or is NOT 22, do something else. (10, 12, 13 ....20, 21, 23, 24...)

I got to do only with 11, but I have no clue how to insert another condition with 22.

if (n >= 0 && n < 10) || (n == 11 && n == 22) {
        do something

    } else if (n >= 10 && n != 11) && (n != 22) {
        do something else
    }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try (n >= 1 && n <= 9) || n == 11 || n == 22 which will check if the number is between 1 and 9 inclusive (1 to 9) OR it's 11 OR it's 22.

Then, in your else if, just check for >= 10 - numbers less than 1 e.g. 0, won't trigger the else either.

if ((n >=1 0 && n <= 9) || n == 11 || n == 22) {
    do something
} else if (n >= 10) {
    do something else
}

Use >= and <= to make it clearer what values you're looking for otherwise it takes a little extra mental load to realise that n > 0 && n < 10 really means values 1 to 9.

Also, it's JavaScript and numbers can either be integral or floating point, so greater than zero could be 0.1 etc.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Based on your example of (1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22) it sounds like this is what you're going for?

if ((n > 0 && n < 10) || n == 11 || n == 22) {
  // do something
} else if (n >= 10) {
  // do something else
}

Edit: Added else if (n >= 10) that I overlooked

about 4 years ago · Juan Pablo Isaza Denunciar

0

In your if statement, you say:

n is larger or equal to 0 AND n is smaller than 10 OR n is equal to 11 AND n is equal to 22

Which means, the if block will run for numbers that are between 0 and 9 (inclusive) OR for the numbers 11 AND 22. So the number can either be between 0 and 9 or it can be BOTH 11 and 22, which is impossible.

My take on the if statement:

if (n >= 1 && n <= 9 || n == 11 || n == 22) {
    do something;
}

That way, you don't get confused with the inclusive and exclusive numbering and make sure that either 11 or 22 is allowed.

The second block will run for numbers that are greater or equal to 10 AND not 11 AND not 22. Which is just fine.

else if (n >= 10 && n != 11 && n != 22) {
    do something else;
}

In this answer, I assume you don't want to entirely skip the 10. If you do, then in the second statement, you would check for n > 10.

Logically, this is sound. However, I don't think that you need to check for 11 and 22 in the second one, as the if block will run first and if the number is 11 or 22, the first if block will run. So the second one doesn't need to account for them.

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