Logical Order in If Else Statements

Logical Order in If Else Statements
0

#1

I made one change t
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(4);
I don’t see anything wrong with code


#2

Read it again and test each condition. if val = 4 and you do

if (val < 10) {
it returns true because 4 < 10 and the code stops here, it does not reach the next condition.
}


#3

this is my code

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);
this wha they want me to change

orderMyLogic(4) should return “Less than 5” i still don’t understand what you are saying


#4

the closing } is deleted i don’t why its showing when i paste


#5

Your conditional logic fails, once the first condition is met, is true, that’s where the code stops. And the first condition that is true in your code is 4 < 10, your second is 4 < 5 but since the first one is met that’s where the code stops.


#6

I still don’t what you think i should do


#7

The order is wrong … just understand what your code does :slight_smile:


#8

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" <_________________________________________________________

#9

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


#10

@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.


#11

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


#12

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


#13

“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.


#14

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


#15

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


#16

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


#17

you need to change the order


#18

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


#19

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

#20

thanks so very much for your help