# Logical Order in If Else Statements Ayuda

Logical Order in If Else Statements Ayuda
0

#1

Tell us what’s happening:
Hola, me falta solo una condicion para superar la prueba :
orderMyLogic(4) should return “Less than 5”
What should I change in the code to overcome the challenge?
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";
}
}

// Change this value to test
orderMyLogic(4);

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/logical-order-in-if-else-statements

#2

Do you understand why it is returning Less than 10 and not Less than 5 as you expect?

#3

No I do not understand.
Of the three options, I overcome two:
orderMyLogic (6) should return “Less than 10”
orderMyLogic (11) should return “Greater than or equal to 10”
And I’m missing the following:
orderMyLogic (4) should return “Less than 5”
I do not understand what I do wrong …
Thanks for the support.

#4

4 también es menor que 10. Recuerde “Orden lógico en caso de otras declaraciones”

4 is also less than 10. Remember “Logical Order in If Else Statements”

#5

When you call orderMyLogic(4) we start going through conditions. The first one:

if (val < 10) {
return "less than 10"
}

Is 4 less than 10? Yes. It will return “Less than 10.”

Perhaps one thing to remember is that return will stop the function and anything else will not be evaluated.

The order makes a difference.

So given that information, can you try to fix it?

#6

I do not finish understanding it, what you tell me, I review it again and again, I’ve been with the problem since yesterday, without success.
Also try with this option, among many others and I fail …
I know that if the first part is fulfilled, the code stops, but I do not know how to overcome …
function orderMyLogic(val) {
if (val > 5 == 10) {
return “Less than 10”;
} else if (val < 5) {
return “Less than 5”;
} else {
return “Greater than or equal to 10”;
}
}

// Change this value to test
orderMyLogic(7);

#7

function orderMyLogic(val) {
if (val > 5 == 10) {
return “Less than 10”;
} else if (val < 5) {
return “Less than 5”;
} else {
return “Greater than or equal to 10”;
}
}

// Change this value to test
orderMyLogic(11);

And in this way either, and should run down

#8

Again, the order is important.

In your second example val > 5 == 10 is not going to work.
You’re pretty close in your original post. You just need a different order for the logic!

#9

And what, I do not finish to understand it?
I’m sure it’s very simple, but I’m not able to get it out.

#10

That’s okay!

The exercise is attempting to teach you the importance of logic order. That means - the order that you have if-else if conditions. It can make a big difference.

It gives two functions:

function foo(x) {
if (x < 1) {
return "Less than one";
} else if (x < 2) {
return "Less than two";
} else {
return "Greater than or equal to two";
}
}

And the second just switches the order of the statements:

function bar(x) {
if (x < 2) {
return "Less than two";
} else if (x < 1) {
return "Less than one";
} else {
return "Greater than or equal to two";
}
}

While these two functions look nearly identical if we pass a number to both we get different outputs.

foo(0) // "Less than one"
bar(0) // "Less than two"

Do you see that the bar function gives the wrong output? The reason is that the order of the conditions is wrong. The order of conditions in foo() is correct. Do you see the difference?

#11

I will continue studying it, I have to overcome the challenge.
And most importantly, understand the logic.
Thank you.

#12

function orderMyLogic(val) {
if (val < 10 && val > 5) {
return “Less than 10”;
} else if (val < 5) {
return “Less than 5”;
} else {
return “Greater than or equal to 10”;
}
}

#13

But I do not have it very clear, it was a coincidence

#14

That worked, didn’t it!
But this is asking about order not about the test of the condition. That means - you don’t need to change var < 10 or var < 5.

Originally it gave you this order:

1. if ( var < 10 )...
2. else if ( var < 5 )...
3. else ...

It’s asking you to rearrange the order.

3. else ...
2. if ( var < 5 )...
1. else if ( var < 10 )...

^^ It’s a different order. I purposely made it wrong. But I hope you get the point!

Anyway, I’m sorry but it’s late here and now I need to sleep!

#15

Thank you very much for the support, and sorry for having spent your time. Think that you helped someone to understand something.
Thank you very much.

#16

return exits your function immediately - no more function code is executed.

// program as written
function orderMyLogic(val) {
if (val < 10 ) {          //first test - is 4 less than 10? Yes it is so...
return "Less than 10";  // ...this line executes return statement and function ends here
// since your function has ended nothing below this line is ever executed.

} else if (val < 5) {    // this test never happens
return "Less than 5";
} else {
return "Greater than or equal to 10";
}
}

console.log(orderMyLogic(4));  //"Less than 10" - not the answer we wanted
// the most correct response would be "Less than 5"

#17

function orderMyLogic(val) {
if (val < 10 && val > 5) {
return “Less than 10”;
} else if (val < 5) {
return “Less than 5”;
} else {
return “Greater than or equal to 10”;
}
}
// Change this value to test
orderMyLogic(4);

#18

Hi,
You don’t need to change the tests. You need to reorder the tests so that you get the correct response. This is a logic problem. What order do you need to ask the questions if(test) to always get the most correct answer?

Rethinking that logic

Let’s try this with the order of your program
Is 4 less than 10? Yes it is. The answer is “Less than 10”
But shouldn’t the answer have been “Less than 5”?
Maybe we asked the wrong question first.

Let’s reorder the questions to get a better answer
Is 4 less than 5? Yes it is. The answer must be “Less than 5”

Let’s try that new order with val = 6
Is 6 less than 5? No
Is 6 less than 10? Yes it is. The answer is “Less than 10”

Let’s try that new order with val = 12
Is 12 less than 5? No
Is 12 less than 10? No
So then 12 must be “Greater than or equal to 10”

So the order of questions probably should be
Is val less than 5? If yes return “Less than 5”. If not, skip
Is val less than 10? If yes, return “Less than 10”. If not, skip
There are no other questions so val must be “Greater than or equal to 10”

Below is code that will spoil the answer to the challenge. Try to fix your code before looking at this.

////////////////////////////////////
// improved - let's try 4 again
function orderMyLogicImproved(val) {
if (val < 5 ) {          //first test - is 4 less than 5? Yes it is so...
return "Less than 5";  // ...this line executes return statement and function ends here
// execution ends here with return statement but this time returns correct response

} else if (val < 10) {
return "Less than 10";
} else {
return "Greater than or equal to 10";
}
}

// Change this value to test
console.log(orderMyLogicImproved(4)); //"Less than 5" -best response - correct!

////////////////////////////////////
// same as above - let's try 10
function orderMyLogicImproved(val) {
if (val < 5 ) {          //first test - is 10 less than 5? No it is not so...
return "Less than 5";  // ...this line is skipped
} else if (val < 10) {   // second test - is 10 less than 10? No it is not so...
return "Less than 10"; // ... this line is skipped
} else {                  // no more test so...
return "Greater than or equal to 10"; // this return statement executes sending correct response
}
}

// Change this value to test
console.log(orderMyLogicImproved(10));  //"Greater than or equal to 10" - correct!

Good luck!