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

151
Vistas
Difficulties getting an if...else statement to work inside a function

I know I am very close, but I can't get the code to return anything but the 'savesTheDay' variable in the else option.I don't think I'm setting up my if....else conditionals properly. I think should be going with the "happy ending" for the if statement (in my case, the savesTheDay variable? Should I not define 'dangerLevel' inside the function since I want it to change? Thanks!

**Edited: I am trying to get each of the phases to return when I call the assessSituation function. I can only get the saveTheDay message to return, even if the value is outside of the parameters of the condition i.e. assessSituation(1);. I'd expect to get the "Meh. Hard Pass." return value instead of saveTheDay.

Added new code, still isn't running though!**


function assessSituation (dangerLevel, saveTheDay, badExcuse) {

  var saveTheDay = "Crime won't stand a chance against me!";
  var dangerLevel = dangerLevel; 
  var badExcuse = "I'm scared, I've never gone up against this many villains";
  
  if (10 < dangerLevel < 50) {
    console.log(saveTheDay);
  } else if (dangerLevel > 50) {
      console.log(badExcuse);
    } else 
    console.log("Meh. Hard Pass.");
    }


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

0

JavaScript, like many other languages (C/C++, Java, etc.) except Python, does not have support for chaining comparison operators.

10 < dangerLevel < 50

is equivalent to

(10 < dangerLevel /* true or false */) < 50
(true) < 50
// or
(false) < 50

Since false == 0 and true == 1, the result will always be true since 0 and 1 are both less than 50.

To fix your logic, use the && (AND) operator:

if ((10 < dangerLevel) && (dangerLevel < 50))
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think it's only returning saveTheDay because it's the only conditional that is matched.

Your dangerLevel = 10, so dangerLevel is not > 50, and it's not smaller than 10, but it's definitely < 50. If you want another return you can change dangerLevel before if else clause.

Ps: I didn't see that your variable inside your function has the same name of the input. You should choose another name if you want to have both

about 4 years ago · Juan Pablo Isaza Denunciar

0

you are setting dangerLevel = 10;

it doesn't matter what you pass in, 10 gets populated for dangerLevel, so:

10 will never be > 50 will never be <10 wil always be < 50

which is why you always get savesTheDay

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