Nodejs Array Check isInt()

Here you can find the source of isInt()

Method Source Code

// //Implement the following custom Array methods:
// even//[1,2,3,4,5].even() should return [2,4];
// odd // [1,2,3,4,5].odd() should return [1,3,5];
// under // [1,2,3,4,5].under(4) should return [1,2,3];
// over // [1,2,3,4,5].over(4) should return [5];
// inRange // [1,2,3,4,5].inRange(1,3) should return [1,2,3];

// //They should also be chainable
// //Filters should only accept integer values from an array

//Solution/* ww w.j  a  v a 2 s  . c  om*/

Array.prototype.isInt = function (){
   return this.filter(function(n){ return (typeof(n) === 'number') && (n%1 === 0)})
};

Array.prototype.even = function (){
   return this.isInt().filter(function(n){ return n%2 === 0})
};

Array.prototype.odd = function (){
   return this.isInt().filter(function(n){ return n%2 !== 0})
};

Array.prototype.under = function (x){
   return this.isInt().filter(function(n){ return n < x})
};

Array.prototype.over = function (x){
   return this.isInt().filter(function(n){ return n > x})
};

Array.prototype.inRange = function (min, max){
   return this.isInt().filter(function(n){ return n >= min && n <= max})
};

Related

  1. isArray(obj)
    Array.isArray = Array.isArray || function (obj) {
      return {}.call(obj) === '[object Array]';
    };
    
  2. isBlank()
    function proxy(func, context, args) {
        return function () {
            return func.apply(context, args);
    Array.prototype.isBlank = function () {
        return !this.length;
    };
    
  3. isCharPresent(charSearch)
    Array.prototype.isCharPresent = function(charSearch) {
      var arr = this;
      var result = [];
      var testingStr = '';
      if (charSearch.length > 1) {
        return "You're searching for more than one char";
      for (var i = 0; i < this.length; i++) {
        if (arr[i].indexOf(charSearch) === -1) {
    ...
    
  4. isDefined(key)
    Array.prototype.isDefined = function (key) {
        return this.filter(c => {
            if (key) return c[key];
            return !!c;
        });
    
  5. isDistinct()
    Array.prototype.isDistinct = function() {
      this.sort();
      for (var i = 1; i < this.length; i++) {
        if (this[i - 1] == this[i])
          return false;
      return true;
    };
    
  6. isLastIndex(index)
    Array.prototype.isLastIndex = function(index) {
      return index == (this.length - 1);
    
  7. isMember(b)
    Array.prototype.isMember = function (b) {
      return this.some(function (i) {
        return this == i
      }, b);
    
  8. isNFromLastIndex(index, n)
    Array.prototype.isNFromLastIndex = function(index, n) {
      return (this.length - 1 - n) == index;
    
  9. isNothing()
    Array.prototype.isNothing = function() {
      return this.length === 0;
    };