Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negation operator (!) used on a recursive call?

I can't figure out how this recursive call works. Using the not operator in the recursive call somehow makes this function determine if the argument given is odd or even. When the '!' is left out fn(2) and fn(5) both return true.

This example is taken out of JavaScript Allonge free e-book, which, so far has been excellent.

var fn = function even(n) {
  if (n === 0) {
    return true;
}
  else return !even(n - 1);
}

fn(2); //=> true

fn(5); //=> false
like image 644
cleversprocket Avatar asked Nov 10 '13 21:11

cleversprocket


People also ask

Which operator is used in recursion?

So, here, the first recursive call takes place. If it returns a logical true, nothing further happens, otherwise the second recursive call takes place, then. What the || operator does here is exactly the same thing it does in any other expression.

How do you end a recursive call?

A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can end up in an infinite loop, if the base case is not met in the calls.

What are recursive functions give three examples?

Simple examples of a recursive function include the factorial, where an integer is multiplied by itself while being incrementally lowered. Many other self-referencing functions in a loop could be called recursive functions, for example, where n = n + 1 given an operating range.


2 Answers

  1. If n === 0 the result is true.
  2. If n > 0 it returns the inverse of n - 1.
    If n === 1 it will return !even(0), or false.
    If n === 2 it will return !even(1), or !!even(0), or true.
    If n === 3 it will return !even(2), or !!even(1), or !!!even(0), or false.
    And so on...

In general:

  • If n is even, the result is inverted an even number number of times, meaning it will return true.
  • If n is odd, the result is inverted an odd number number of times, meaning it will return false.
like image 54
p.s.w.g Avatar answered Oct 21 '22 01:10

p.s.w.g


The above function reurns recursively the negation of it self.The base-case is when the number provided becomes zero and each time the function calls it self the number is decreased by one. As a result we have n recursive negations starting with true at base-case (where n is the number provided). For an odd number of negations given true as a starting value you get false as the result and for an even number you get true. In summary:

  1. Starting from given n
  2. recursive reduction of n
  3. Basecase: n=0 returns true
  4. recursive negation of returned value(starting from true at base-case) Result:
    • for odd number of negations the value returned is false
    • for even number of negations the value returned is true

Lets say we have example n=5
recursive reduction of n. Values of n at each level:
5
  4
    3
      2
        1
          0 (base-case)
returned values at each level:
          true (base case)
        !true
      !!true
    !!!true
  !!!!true
!!!!!true

like image 35
Andreas Avatar answered Oct 21 '22 01:10

Andreas