dimanche 31 juillet 2016

were does this function gets its values?

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:

  1. 42 binds to n
  2. Creating "even" function
  3. x binds to n which = 42
  4. x != 0
  5. initiating "else"
  6. creating "odd" function
  7. odd(42 - 1)
  8. 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