Javascript Array classifyNumbers()

Description

Javascript Array classifyNumbers()


Array.prototype.classifyNumbers = () => {
    this.reduce((result, item) => {// w w  w.  j a  v a2s  .c om
        if (item % 2 == 0)
            result.pairs.push(item);
        else
            result.impairs.push(item);

        return result;
    }, {
        pairs: [],
        impairs: []
    })
};

Object.defineProperty(Array.prototype, "isEmpty", {
  get: () => this.length == 0,
  configurable: false
});

var arr = [1, 3, 44];
var x = arr.classifyNumbers();
if(x.isEmpty) {

}

Javascript Array classifyNumbers()



Array.prototype.classifyNumbers = () => {
  this.reduce((result, item) => {//from ww w  .  j  a v a2s  . com
    if(item % 2 == 0){
      result.pairs.push(item);
    }else{
      result.impairs.push(item);
    }
    return result;
  }, {
    pairs: [],
    impairs: []
  })
};


Object.defineProperty(Array.prototype, "isEmpty",{
  get: () => this.length == 0,
  configurable: false //propiedad de solo lectura
});

var arr = [1,55];
var x = arr.classifyNumbers();

Javascript Array classifyNumbers()



Array.prototype.classifyNumbers = () => {
    this.reduce((result, item) => {/*from   ww w . j  a  v a2 s. c om*/
        if (item % 2 == 0)
            result.pairs.push(item);
        else
            result.impairs.push(item);

        return result;
    }, {
        pairs: [],
        impairs: []
    })
};

var arr = [3, 4, 1, 39, 11];


var x = arr.classifyNumbers();

console.log("x: " + x);

if(x.isEmpty){

}



PreviousNext

Related