Mutual recursion (even / odd)

Medium TimeO(n) SpaceO(n)

Given a number n, zero or greater, decide whether it is even. Neither function may use modulo or division, and the answer has to come from two functions that call each other. 0 is even, and is not odd.

Examples

Example 1

Input
n = 6
Output
true
6 = 2 × 3, so the answer is true.

Example 2

Input
n = 7
Output
false
7 leaves one over when paired off, so the answer is false.

The Code

function isEven(n) {
  if (n === 0) return true;
  return isOdd(n - 1);
}
function isOdd(n) {
  if (n === 0) return false;
  return isEven(n - 1);
}
isEven(6);
Done
Step through isEven(6) call by call