Nodejs Number Value Check isEven()

Here you can find the source of isEven()

Method Source Code

/**//from ww w  .  ja va 2  s. c  o  m
 * Problem #2 - Even Fibonacci numbers
 *
 * Each new term in the Fibonacci sequence is generated by adding the previous two terms.
 * By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

 * By considering the terms in the Fibonacci sequence whose values do not exceed four million,
 * find the sum of the even-valued terms.
 *
 * Answer: 
 */
 
window.log = console.log;
 
function fib(num) {
  return (num < 3) ? num : fib(num - 1) + fib(num - 2);
}

Number.prototype.isEven = function() {
  return (this % 2 === 0) ? true : false;
};

function fibSum() {
  var i = 1,
      sum = 0,
      fibValue = fib(i),
      limit = 4000000; // 4 million
      
  while (fibValue < limit) {
    if (fibValue.isEven()) {
      sum += fibValue;
    }
    i++;
    fibValue = fib(i);  
  }
  return sum;
}

var evenFibSum = fibSum();

log(evenFibSum);

Related

  1. isEven()
    Number.prototype.isEven = function(){
      return this % 2 === 0;
    
  2. isEven()
    Number.prototype.isEven = function (){
      return this.isOdd() == false;
    
  3. isEven()
    Number.prototype.isEven = function () { return (this % 2) === 0; }; 
    Number.prototype.isOdd = function () { return Math.abs(this % 2) === 1; };
    Array.prototype.isEmpty = function () { return this.length === 0; };
    Array.prototype.hasItem = function (item) { return this.indexOf(item) >= 0; };
    Array.prototype.remove = function (item) {
      var index = this.indexOf(item);
      if (index >= 0)
        this.splice(index, 1);
    };
    ...
    
  4. isEven()
    Number.prototype.isEven = function () {
        if (this % 2 === 0) {
            return true;
        } else return false;
    };
    function sumFibs(num) {
        var sumFibArr;
        var fibArray = fibArrayGenerator(num);
        fibArray = fibArray.filter(function (value) {
    ...
    
  5. isEven()
    Number.prototype.isEven = function() {
        return !(this % 2);
    
  6. isOdd()
    Number.prototype.isOdd = function () {
      return new Boolean(this.valueOf()&1);
    
  7. isOdd()
    function isOdd(num) {
      if (typeof num === 'number') {  
        return ((num % 2) === 1) || ((num % 2) === -1) ? true : false;
      else {
        throw 'Not a number';
    console.log(isOdd(3)); 
    ...
    
  8. isOdd()
    Number.prototype.isOdd = function() {
        return !!(this % 2);