Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!