Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda