Array Helpers code example

Example: Array Helpers

var numbers = [1, 2, 3, 4, 5]

  Array.prototype.square = function(){ return this.map(num => num ** 2) }
  
  Array.prototype.cube = function(){ return this.map(num => num ** 3) }
  
  Array.prototype.sum = function(){ return this.reduce((acc, curr) => acc + curr , 0) }
  
  Array.prototype.average = function(){
    return this.reduce((acc, curr) => acc + curr , 0) / this.length
  }
  Array.prototype.even = function(){ return this.filter(num => num % 2 === 0) }
  
  Array.prototype.odd = function(){ return this.filter(num => num % 2 !== 0) }

console.log(numbers.square())

Tags:

Misc Example