recursion - Recursive even function issue with understanding (Javascript) -
the problem simple, have function 'javascript allonge' book, , having hard time in understanding it.
the function called even, , it's follows:
var = function(num) {     return (num === 0) || !(even(num -1)); } it checks whether number or not, not understand how. calls recursively, , technically, reaches zero, no? how work?
this based on inductive definition of numbers being odd or - number, n 'even' when number before it, n - 1 odd.  thinking naturally makes sense - 4 if 3 odd.
and function even defined as:
1. even(0) true - because 0 even
2. even(n) negation of even(n - 1)
another way think of imagine even(4) being called step step.  hand, replace even(4) result of evaluation function:
even(4) = !(even(3)) = !(!even(2)) = !(!(!even(1)) = !(!(!(!even(0))) = !(!(!(!true)) = true   // ...even(4) == true 
Comments
Post a Comment