Javascript Array square()

Description

Javascript Array square()


Array.prototype.square = function(){
  return this.map(e=>e*e);
}

Javascript Array square()

Array.prototype.square = function(){
  return Array.from(this, x => x * x)
}

Javascript Array square()

Array.prototype.square = function(){
   return this.map((x)=>Math.pow(x,2));
}

Javascript Array square()

Array.prototype.square = function(){
    return this.map((element) =>  Math.pow(element,2));}

Javascript Array square()

Array.prototype.square = function () {
  return this.map(function(n) { 
    return n * n; 
  });/*  w  w  w. j av a 2s .  c o m*/
}

Javascript Array square()

//---- Array Helpers ----//

//Helper methods added to the Array.protoype using map, filter, and reduce 

Array.prototype.square = function() { return this.map( n => n * n)};

Javascript Array square()

// TODO//from  w ww .  j  a  va  2  s.c  o m
Array.prototype.square = function(){
   return   this.map(function(e){return e*e});
}

Javascript Array square()

Array.prototype.square  = function () { return this.map(function(n) { return n*n; }); };

Javascript Array square()

Array.prototype.square = function() {
    return this.map(function(e) {
        return Math.pow(e, 2)
    })/*  w ww  . j a v  a2s  . c  om*/
};

Javascript Array square()


Array.prototype.square = function () {
  return this.map(function(n) {
    return n*n;/*from  www  . j a v  a 2 s.co m*/
  });
}

Javascript Array square()

Array.prototype.square = function () {
  return this.map((item) => {
    return Math.pow(item, 2);
  });/*from w w  w .  j av  a2 s .  c o  m*/
};

Javascript Array square()

Array.prototype.square = function() {
  return this.map(function(number) {
    return Math.pow(number,2);
  });//from   ww  w.  j av a2  s .c  om
}

Javascript Array square()

Array.prototype.square = function() {
    return this.map(function(num) {
        return Math.pow(num,2);
    });//from w  w w . j ava2 s .co  m
}

Array.prototype.cube = function() {
    return this.map(function(num) {
        return Math.pow(num,3);
    });
}

Array.prototype.sum = function() {
    return this.reduce(function(num1, num2) {
        return num1 + num2;
    }, 0);
}

Array.prototype.average = function() {
    return this.sum() / this.length;
}

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

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

Javascript Array square()

Array.prototype.square = Square;
Array.prototype.cube = Cube;
Array.prototype.average = Average;
Array.prototype.sum = Sum;
Array.prototype.even = Even;
Array.prototype.odd = Odd;

function Square() {
    return this.map(function(num) {
        return num * num;
    });//from  w  ww  . j  a v  a2  s.  com
}

function Cube() {
    return this.map(function(num) {
        return num * num * num;
    });
}

function Average() {
    return this.sum() / this.length;
}

function Sum() {
    return (this == '') ? 0 : this.reduce(function(a, b) {
        return a + b;
    });
}

function Even() {
    return this.filter(function(num) {
        return num % 2 == 0;
    });
}

function Odd() {
    return this.filter(function(num) {
        return num % 2 != 0;
    });
}

Javascript Array square()

Array.prototype.square = function() {
    var arr = [];
    for(var i = 0; i < this.length; i++) arr[i] = Math.pow(this[i], 2);
    return arr;/*w  w w.  java2s  .  co m*/
};

Javascript Array square()

// Numbers Square
var square = (el) => Math.pow(el,2);
var cube = (el) => Math.pow(el,3);
var isEven = (el) => (el % 2 === 0) ? true : false;
var isOdd = (el) => (el % 2 !== 0) ? true : false;


Array.prototype.square = function(){
  return this.map(square);
};

Array.prototype.cube = function(){
  return this.map(cube);
};

Array.prototype.sum = function(){
  if (this.length === 0) {return 0;}
  return this.reduce((sum,el)=>sum+=el);
};

Array.prototype.average = function(){
  return this.sum() / this.length;
};

Array.prototype.even = function(){
  return this.filter(isEven);
};

Array.prototype.odd = function(){
  return this.filter(isOdd);
};

Array.prototype.adder = function(num){
  return this.map(el => el + num);
};

Array.prototype.checkNum = function(num){
  if (this.indexOf(num) === -1) {
    return false;
  } else {/*from ww w . ja va2  s .  c  o m*/
    return true;
  }
};

module.exports = {Array};

Javascript Array square()

Array.prototype.square = function(){
    return this.map(function(e){
        return (e * e);
    });//from   w  w  w .j  a  v a 2 s. co m
};

Array.prototype.cube = function(){
    return this.map(function(e){
        return (e * e * e);
    });
};

Array.prototype.sum = function(){
    return this.reduce(function(a,b){
        return a+b;
    });
};

Array.prototype.even = function(){
    var niz = [];

    this.forEach(function(a) {
        if (0 == a % 2) {
            niz.push(a);
        }
    });

    return niz;
};
Array.prototype.odd = function(){
    var niz = [];

    this.forEach(function(a) {
        if (0 != a % 2) {
            niz.push(a);
        }
    });

    return niz;
};


var niz1 = [1,3,4,5,6];
console.log(niz1.square());
console.log(niz1.cube());
console.log(niz1.sum());
console.log(niz1.even());
console.log(niz1.odd());

Javascript Array square()

Array.prototype.square = function (){
  var result = this.map(function(v){
      return v*v;
  });/* w ww  .  j  a  v a  2  s  . c  o m*/
  return result;
};

Javascript Array square()

Array.prototype.square = function() {
  return this.map(function(num){
  return Math.pow(num, 2);
 }); //www.j a v a 2  s .com
}

Array.prototype.cube = function() {
 return this.map(function(num){
  return Math.pow(num, 3);
 }); 
}

Array.prototype.sum = function() {
 return this.reduce(function(a,b){
  return a + b;
 }); 
}

Array.prototype.average = function() {
 return this.sum() / this.length;
}


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

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


var numbers = [1, 2, 3, 4, 5];
console.log(numbers.square());

console.log(numbers.average());

console.log(numbers.even());

Javascript Array square()

Array.prototype.square = function()
{
  var ret = new Array();
  
  this.forEach(function(a) {
    ret.push(a * a);/*from  ww w  . j  ava 2s. c  o m*/
  });
  
  return ret;
}

Array.prototype.cube = function()
{
  var ret = new Array();
  
  this.forEach(function(a) {
    ret.push(a * a * a);
  });
  
  return ret;
}

Array.prototype.sum = function()
{
  if (this.length < 1) {
    return Number.NaN;
  }
  
  var sum = 0;
  this.forEach(function(a) {
    sum += a;
  });
  
  return sum;
}

Array.prototype.average = function()
{
  if (this.length < 1) {
      return Number.NaN;
  }
  
  return this.sum() / this.length;
}

Array.prototype.even = function()
{
  var ret = new Array();
  
  this.forEach(function(a) {
    if (0 == a % 2) {
      ret.push(a);
    }
  });
  
  return ret;
}

Array.prototype.odd = function()
{
  var ret = new Array();
  
  this.forEach(function(a) {
    if (0 != a % 2) {
      ret.push(a);
    }
  });
  
  return ret;
}

Javascript Array square()

"use strict";/* w w w.j  a v  a 2  s.c o  m*/

Array.prototype.square  = function(){

  return this.map(function(element){
    return element*element;
  });
};



PreviousNext

Related