Skip to content
Advertisement

Why do I get a missing return statement error although there is a return statement in each of my if and if else blocks?

What I am trying to do here out of flower1 and flower2, one is even and one is odd then return true. If both are even, return false. If both are odd, return false. When my code is:

JavaScript

I get a “missing return statement” error. So I added:

JavaScript

Now, the code works but I do not understand why I have to add the additional return statement.

Advertisement

Answer

The compiler doesn’t know the first 3 terms cover all situations.

JavaScript

to you this reads as: all options are covered. but the compiler just sees:

JavaScript

You may know that the last else if should always be true (since you know they are not both uneven) but the compiler doesn’t generally try to understand your code.

in your case, the last clause (aThirdThingThatMayBeTrue, flower1%2==0 || flower2%2==0) is actually (somethingThatIsAlwaysTrueIfPreviousTermsAreFalse).

so you can treat it as such:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement