Skip to content

Latest commit

 

History

History
105 lines (89 loc) · 3.01 KB

array-helpers.md

File metadata and controls

105 lines (89 loc) · 3.01 KB

Description

This kata is designed to test your ability to extend the functionality of built-in ruby classes. In this case, we want you to extend the built-in Array class with the following methods: square(), cube(), average(), sum(), even() and odd().

Explanation:

  • square() must return a copy of the array, containing all values squared, the original array must not be changed
  • cube() must return a copy of the array, containing all values cubed, the original array must not be changed
  • average() must return the average of all array values, average() on an empty array must return NaN
  • sum() must return the sum of all array values
  • even() must return an array of all even numbers, the original array must not be changed
  • odd() must return an array of all odd numbers, the original array must not be changed

Examples:

var numbers = [1, 2, 3, 4, 5];
numbers.square(); // must return [1, 4, 9, 16, 25]
numbers.cube(); // must return [1, 8, 27, 64, 125]
numbers.average(); // must return 3
numbers.sum(); // must return 15
numbers.even(); // must return [2, 4]
numbers.odd(); // must return [1, 3, 5]

Kata's link: Array Helpers

Best Practices

First:

Array.prototype.square  = function () { return this.map(function(n) { return n*n; }); }
Array.prototype.cube    = function () { return this.map(function(n) { return n*n*n; }); }
Array.prototype.average = function () { return this.sum() / this.length; }
Array.prototype.sum     = function () { return this.reduce(function(a, b) { return a + b; }, 0); }
Array.prototype.even    = function () { return this.filter(function(item) { return 0 == item % 2; }); }
Array.prototype.odd     = function () { return this.filter(function(item) { return 0 != item % 2; }); }

My Solutions

function checkArr(arr){
  return Array.isArray(arr) || Object.prototype.toString.call(arr) === '[object Array]';
}

let arrProto = Array.prototype;

arrProto.square = function(){
  if(checkArr(this)){
     return this.map((item) => {
        return Math.pow(item, 2);
     });
  } else {
    throw 'params must be a array';
  }
};

arrProto.cube = function(){
  if(checkArr(this)){
     return this.map((item) => {
        return Math.pow(item, 3);
     });
  } else {
    throw 'params must be a array';
  }
};

arrProto.sum = function(){
  if(checkArr(this)){
     return this.reduce((previousValue, currentValue) => {
        return previousValue + currentValue;
     },0);
  } else {
    throw 'params must be a array';
  }
};

arrProto.average = function(){
  if(checkArr(this)){
     return this.sum() / this.length;
  } else {
    throw 'params must be a array';
  }
};

arrProto.even = function(){
   if(checkArr(this)){
     return this.filter((item) => {
        return item % 2 === 0;
     });
  } else {
    throw 'params must be a array';
  }
};

arrProto.odd = function(){
   if(checkArr(this)){
     return this.filter((item) => {
        return item % 2 !== 0;
     });
  } else {
    throw 'params must be a array';
  }
};