This is an example from a book. The function returns TRUE if even and FALSE if not. I cant understand how it works. This is what I understand:
- 42 binds to n
- Creating "even" function
- x binds to n which = 42
- x != 0
- initiating "else"
- creating "odd" function
- odd(42 - 1)
- Initiating "!even(41)".
What does JS do with "even(41)"? were TRUE comes from? The way I understand it should return TRUE only when x === 0
document.write(
((n) => {
const even = (x) => {
if (x === 0) return true;
else {
const odd = (y) => !even(y);
return odd(x - 1);
}
}
return even(n)
})(42)
)
Aucun commentaire:
Enregistrer un commentaire