Basic JavaScript - Logical Order in If Else Statements

Tell us what’s happening:
Describe your issue in detail here.
wont check should return the string less than 5 but the other two pass

  **Your code so far**
function orderMyLogic(val) {
if (val < 10) {
  return "Less than 10";
} else if (val < 5) {
  return "Less than 5";
} else {
  return "Greater than or equal to 10";
}
}

orderMyLogic(11);
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14816.131.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Logical Order in If Else Statements

Link to the challenge:

When the number 3 or 8 is passed to the function as: orderMyLogic(3) it returns "Less than 10". That is because the if-else if-else statement evaluates from top to bottom, starting from the first if. If the condition doesn’t match, the next else-if, and so on, and finally the last else.

Since, the first if condition evaluates to true, the number 3 is "Less than 10". So, how to make this work correctly? Re-arrange the order in which the conditions are now, checking for the least values first (and see how it works; you can try various orders and understand the behaviuor of the code).

You will change your function to become like this

Mod Edit: SOLUTION REMOVED

and run the test!

then you will change argument of your function to 6, and run it again, in the end you will change your argument to 11, and run it!
then you will done!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

Do not revert modertion edits of your posts.