Javascript Number isNumber()

Description

Javascript Number isNumber()


// http://www.freecodecamp.com/challenges/bonfire-arguments-optional

Number.prototype.isNumber = function() {
  return (typeof this === 'number');
};

function add() {//from   ww w .  j  av a  2 s .  c o  m
  var val1 = arguments[0],
      val2 = arguments[1];

  if (val1.isNumber && arguments.length === 1) {
      return function(num) {
          if (num.isNumber) { return val1 + num; }
          return undefined;
      };
  } else if (val1.isNumber && val2.isNumber) {
    return val1 + val2;
  }
  return undefined;
}

console.log(add(2)([3]));

Javascript Number isNumber()

/**/*from   w  w w .ja v a 2  s. com*/
 * Created on 1/14/2015.
 */
var assert=require('assert');
var Number=function(number){
    this.number = number;
}
Number.prototype.isNumber=function(){
    return isNaN(this.number);
}
Number.prototype.isEven=function(){
    return this.number%2==0;
}
Number.prototype.isOdd=function(){
    return this.number%2!=0;
}

var evenNumber=new Number(12);
console.log("Is number even? "+evenNumber.isEven());

var oddNumber=new Number(13);
console.log("Is number odd? "+isNaN(oddNumber));

var strangeNumber=new Number("strange");

//verification through unit test
assert.equal(evenNumber.isEven(), true);
assert.equal(oddNumber.isOdd(), true);
assert.equal(strangeNumber.isNumber(), true);



PreviousNext

Related