Logical Order in If Else Statements

I understand but the instruction says others wise.

this below is the only question that was asked of me
orderMyLogic(4) should return “Less than 5” I know the code stope 4< 10 was true but i don’t know what to because my test is failing

<!------Below the were changed in green by defailt------------------------------------------------ //orderMyLogic(6) should return "Less than 10" //orderMyLogic(11) should return "Greater than or equal to 10" <_________________________________________________________

When you have an if statement, and its condition is true (For example your first if condition will be true with a input of 4 for val)… Then the program will go inside of that if statement. once if finishes inside of that if statement it will exit. any else if or else are considered to be apart of that first if…

(What @dpberry552 under my post said…)

1 Like

@mangalita

As @Nepherius pointed out, the syntax of your code is correct, but logic is not. The order of when the If statements are executed needs to be changed so that you are not missing potentially valid conditions.

I still don’t understand when i’m just following the examples
I changed one
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);
Still getting error

4 is less than 10 so the function returns “Less than 10”. It never checks if it’s less than 5!

“else if”, is not a separate “if statement”. “else if” is a part of the “if statement” above it… The “else if” condition will only be checked if the previous “if statement” is false.

so. if the first “if” is false, then it will check the next “else / else if”, and if that is false then it will check the one after that(and so on). But if the first “if” is true, it will not check any of the other “else / else if statements” that are after it.

so how can i do that 10 and 5 was given to me

You have to rewrite the code. All of these problems require you to rewrite the code to make it work.

this their code
unction 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(7);
The instructions state to change orderLogic(7)
see the question below
orderMyLogic(4) should return "Less than 5"
here is the link

you need to change the order

you mean like this see below

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

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

this is wrong even when put orderLylogic(7) it is still wrong

more like this:

function orderMyLogic(val) {
if (val < 5) {
return "Less than 5";
} else if (val < 10) {
return "Less than 10";
} else {
return "Greater than or equal to 10";
}
}
3 Likes

thanks so very much for your help

Solution is:

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

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

1 Like

Sorry, that was the wrong solution:

Here’s the right one:

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

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

the problem is the order of the code, hence what the whole chapter is about. when it tests if 4 is less than 10 the first time it is true, so it doesnt continue in the else statements. reorder the code so that it tests if 4 is less than 5 first.

A post was split to a new topic: Need Help with If Else Statement

My solution for this problem. We can use && operator in the first ‘if’ statement

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";
  }
}
2 Likes

Solution is:-
function orderMyLogic(val) {

if (val < 5) {

return “Less than 5”;

} else if (val < 10) {

return “Less than 10”;

} else {

return “Greater than or equal to 10”;

}

}

// Change this value to test

orderMyLogic(4);