Why do i not need to define else for return true and return false?
i don’t think this was explained but when i enter else in to the equation it messes up the code.
var lost = [4, 8, 15, 16, 23, 42];
var count = lost.length;
var isLost = function (n) {
for (i=0;i<=count;i++ ) {
if ( n === lost[i]) {
return true;
}**<--here, there is no else and if i put it in i get an error**
}
return false;
};
if ( isLost(12) ) {
console.log('12 is a lost number');
}
if ( isLost(16) ) {
console.log('16 is a lost number');
}
edit by @Dory: fixed code formatting
Answer 5040c8c5a355380002039357
@Dory and @redemption2021: I’m afraid you’re both missing an important point (see below).
First of all, quite generally,
if (condition) { action } else { action2 }
is not the same as
if (condition) { action } action2
because in the first example action2 will only be run if condition is false, but in the second one it’ll run no matter what.
This changes a bit when we have a return statement in the first action – we can then omit the else because a return will end the whole function so the action2 is also only executed if condition is false. So, the if statement in exercise 3.4 can be rewritten as
if (n%2===0) return true;
/* no explicit "else" */ return false;
(by the way, it’s even shorter to simply say return n%2===0, because what we have now is repetitive: “if true, return true, if false, return false…”).
But all this is not the point when we look at exercise 3.7 “The Lost Numbers”. Here comes
the point:
in 3.7, we want to return false only if no number in the “lost“ array matches n. In other words, only after having checked all the numbers. In yet other words, only after the end of the for loop.
If the for loop finds some number that matches n, the test condition of the if statement will fire and we’ll return true, thereby exiting the loop and the whole function. Only when the loop has actually reached its end, we know we can return false because the number n was not found.
And there’s nothing wrong with an else statement per se, as long as it doesn’t end the loop:
if ( n === lost[i] ) {
return true;
} else { /* don't return anything. just chill */ }
However, it would be wrong to put a return inside that else, because it would get out of the function (and therefore out of the for loop that is inside the function) too early, as soon as it finds an array item that doesn’t match n.
5 comments
This seems like something that should be covered in the lesson plan.
That’s why I gave this lesson a thumb down. It’s simply uncompleted.
byw, great question @redemption2021!
Thanks a lot Alex J! Helped me a lot!
Great explanation, thank you!
Answer 503ff9df6f7bf9000201c6dd
There is no need to add an else statement, because if the if statement doesn’t get run then your function is automatically going to return false because of the line that says
return false;
1 comments
Can you explain how this is different from the exercise on 3.4 Branch and return where the else is placed there for us?
Answer 5040b2d7a3d0e2000203224d
I Googled your question, and what people seem to say about it is that the computer will read it the same, regardless of whether or not you put in the else, but it’s good to put in the else to make it more readable for humans that will be looking at the code. So, I guess for these exercises it was just a matter of the author’s preference. The reason that it doesn’t pass when you do else is the correctness test (that the author wrote), not that your code wouldn’t work.
Answer 50589587542d010002068bae
Popular free courses
- In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
- Beginner Friendly.4 Lessons
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.11 Lessons
- Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
- Beginner Friendly.6 Lessons