How do I relate recursion to the remainder operator?

I’m using Eloquent JavaScript to learn JavaScript. I am a beginner and I would like to understand how this exercise works, it is about recursion and relating in to the remainder operator. I don’t understand the comparison and I really would love to know how it works.

function isEven(n) {
  if (n == 0) return true;
  else if (n == 1) return false;
  else if (n < 0) return isEven(-n);
  else return isEven(n - 2);
}
console.log(isEven(50)) //true;
console.log(isEven(75)) //false;
console.log(isEven(-1)) //false;

I tried testing -2 in the log and it prints true, why does it do that?

I don’t fully understand recursion or JavaScript that much, I would like it if this example is explained to me like I am 5.

36 thoughts on “How do I relate recursion to the remainder operator?”

  1. 151048 166745This is the suitable blog for anybody who needs to seek out out about this topic. You notice so a lot its virtually laborious to argue with you (not that I really would wantHaHa). You undoubtedly put a brand new spin on a topic thats been written about for years. Great stuff, just wonderful! 162439

    Reply

Leave a Comment