-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayHelpers.js
109 lines (82 loc) · 3.01 KB
/
ArrayHelpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This kata is designed to test your ability to extend the functionality of built-in 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
// cube() must return a copy of the array, containing all values cubed
// average() must return the average of all array values; on an empty array must return NaN (note: the empty array is not tested in Ruby!)
// sum() must return the sum of all array values
// even() must return an array of all even numbers
// odd() must return an array of all odd numbers
// Note: the original array must not be changed in any case!
// Example
// 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]
Array.prototype.square = function() {
return this.map((el) => el**2)
}
Array.prototype.cube = function(){
return this.map((el) => el * el * el)
}
Array.prototype.average = function(){
return this.reduce((a, b) => a + b, 0)/this.length
}
Array.prototype.sum = function(){
return this.reduce((a, b) => a + b)
}
Array.prototype.even = function(){
return this.filter((el) => el % 2 === 0)
}
Array.prototype.odd = function(){
return this.filter((el) => el % 2 === 1)
}
//different approach
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; }); }
//different approach
Array.prototype.square = function() {
var squared = [];
for (var i = 0; i < this.length; i++) {
squared[i] = Math.pow(this[i], 2);
}
return squared;
}
Array.prototype.cube = function() {
var cubed = [];
for (var i = 0; i < this.length; i++) {
cubed[i] = Math.pow(this[i], 3);
}
return cubed;
}
Array.prototype.sum = function() {
var sum = 0;
for (var i = 0; i < this.length; i++) {
sum += this[i];
}
return sum;
}
Array.prototype.average = function() {
if (this.length == 0) return NaN;
return this.sum() / this.length;
}
Array.prototype.even = function() {
var evens = [];
for (var i = 0; i < this.length; i++) {
if (this[i] % 2 == 0) evens.push(this[i]);
}
return evens;
}
Array.prototype.odd = function() {
var odds = [];
for (var i = 0; i < this.length; i++) {
if (this[i] % 2 == 1) odds.push(this[i]);
}
return odds;
}