Logical Order in If Else Statements

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);