If I write the following code, JSLint complains that 'isOdd' was used before it was defined. Is there a way to write mutually recursive code and still please JSLint?
var isEven = function(n) {
if (n === 0) {
return true;
}
return isOdd(n - 1);
};
var isOdd = function(n) {
if (n === 0) {
return false;
}
return isEven(n - 1);
};
Converting these functions to methods of an object quell the error message from JSLint. This also does a better job of keeping the global namespace less polluted.
var numUtil = {
isEven: function(n) {
if (n === 0) {
return true;
}
return this.isOdd(n - 1);
},
isOdd: function(n) {
if (n === 0) {
return false;
}
return this.isEven(n - 1);
}
};