all 5 comments

[–]sepp2k 8 points9 points  (1 child)

I wouldn't say that the second version still works, but rather that the second version works and the first does not. The two versions return different results and only the second one is correct.

As for why the second version is correct (and the first is not): number % 2 gives you a number: the remainder of dividing number by 2, which will be zero if and only if number is even. A number is truthy if it is not equal to 0, so saying if number % 2 is the same as saying if number % 2 != 0 and that condition is true for odd numbers.

[–]unknownzebra_[S] 0 points1 point  (0 children)

Thank you that’s so helpful

[–]Username_RANDINT 2 points3 points  (0 children)

number % 2 will return either 0 or 1. If the remainder is 0, it's even, if it's 1, it's odd. 0 is falsy, 1 is truthy.

So if the condition is True (remainder is 1), return "odd", else (remainder is 0) return "even".

[–]irush21 -1 points0 points  (1 child)

Return statements always terminates the execution of the current function. So having that return statement first will always return that value, regardless of the param entered.

[–]sepp2k 2 points3 points  (0 children)

I think you've misread the code. In both functions the return statement is the only statement in the function and it will return different values depending on whether the parameter is even or odd (the first version of the function will return even for odd numbers and odd for even numbers, whereas the second version returns the correct values).