-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
44 lines (38 loc) · 865 Bytes
/
array.js
File metadata and controls
44 lines (38 loc) · 865 Bytes
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
Array.prototype.mean = function(){
return this.reduce(function(previousMean, currentValue, i){
return previousMean + (1/(i + 1))*(currentValue - previousMean);
});
};
Array.prototype.median = function() {
var values = this;
values.sort( function(a,b) {return a - b;} );
var half = Math.floor(values.length/2);
if(values.length % 2){
return values[half];
} else {
return (values[half-1] + values[half]) / 2.0;
}
};
Array.prototype.max = function(){
return this.reduce(function(p,n){
if(n > p || p === null){
return n;
} else {
return p;
}
},null);
};
Array.prototype.min = function(){
return this.reduce(function(p,n){
if(n < p || p === null ){
return n;
} else {
return p;
}
},null);
};
Array.prototype.sum = function(){
return this.reduce(function(p,n){
return p + n;
}, 0);
};