Javascript Number isOdd()

Description

Javascript Number isOdd()

Number.prototype.isOdd = function() {
  return Math.abs(this % 2) == 1;
};

module.exports = Number;//  w  w w .  j av a  2s. c o m

Javascript Number isOdd()

/** Returns true if the number is odd */
Number.prototype.isOdd = function () {
 return new Boolean(this.valueOf()&1);
}

Javascript Number isOdd()

function isOdd(num) {
 if (typeof num === 'number') { 
  return ((num % 2) === 1) || ((num % 2) === -1) ? true : false;
 }
 else {/*from   w  w  w  .  j a  va2  s . co m*/
  throw 'Not a number';
 }
}

console.log(isOdd(3)); // true
console.log(isOdd(2));  // false
console.log(isOdd(-3));  // true
console.log(isOdd(2.1)); // false
console.log(isOdd('3')); // Uncaught Not a number

Number.prototype.isOdd = function() {
  return ((this % 2) === 1) || ((this % 2) === -1) ? true : false;
}


var a = -3;
console.log(a.isOdd());  // true

a = 2.1;
console.log(a.isOdd()); // false

console.log((3).isOdd()); // true
console.log((2).isOdd());  // false

a = '3';
console.log(a.isOdd()); // Uncaught Not a number



PreviousNext

Related