Tell us what’s happening:
really, i can’t understand what’s going on here haha
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(7);
1 Like
What happens is just like in your math days.
a + 6 = ?;
a = 3;
3 + 6 = 9;
Just like you sub the number 3 here for a., you do the same in the function.
focus on these two lines:
function orderMyLogic(val){...}
orderMyLogic(7);
The function uses a variable called val.
So what happens here is val
becomes 7. Everywhere inside the function orderMyLogic that uses val, you substitute it with the number 7.
1 Like
In the future, more specific questions are better.
function orderMyLogic(val) {
This is a function, a block of code that accepts a variable as a parameter, named val.
if (val < 10) {
return "Less than 10";
If val is less than 10, return that string. Once you return that value, the function is over.
} else if (val < 5) {
return "Less than 5";
If we made it past that last if
(else
) then check if val is less than 5, return that string. Again, once you return, you are done with the function.
} else {
If we made it this far (else
) return that string.
At this point you are done with the function because there is no condition where you would not have hit a return
statement.
orderMyLogic(7);
This invokes the function and sends it the value 7, which is what val will be in the function. So, the function will return "Less than 10"
. Because you are not saving that returned value, you won’t be able to see it.
1 Like
I know all of that, but they giving me error:
orderMyLogic(4) should return "Less than 5"
Look at your code.
if (val < 10) {
return "Less than 10";
} else if (val < 5) {
return "Less than 5";
}
if val is 4, then 4 < 10 which means you return “Less than 10”. It never makes it to your 2nd else if condition.
1 Like
val < 10 is:
-infinity … 0 1 2 3 4 5 6 7 8 9
val < 5 is:
-infinity … 0 1 2 3 4
Since they cross over the same #s it’s better to test for the smaller range first.
1 Like
JaMann, danke - es hat funktioniert
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(7);
That’s why you need to ask specific questions.