-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.js
58 lines (52 loc) · 1.16 KB
/
array.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
/**
* Gets the lowest value from a an array of numbers
* @param {Array} arr
*/
export function indexOfMinValue (arr) {
return arr.reduce((iMin, val, i, arr) => val < arr[iMin] ? i : iMin, 0)
}
/**
* Creates an array with values equal to there index
* @param {Array} length
*/
export function generate(length) {
return Array.from(Array(length).keys())
}
/**
* Sums an array of numbers
* @param {Array} arr
*/
export function sum(arr) {
return arr.reduce((sum, value) => sum + value, 0)
}
/**
* Averages an array of numbers
* @param {Array} arr
*/
export function average(arr){
return sum(arr) / arr.length
}
/**
* Calculates variance for an array of numbers
* @param {Array} arr
*/
export function variance(arr){
var avg = average(arr)
var num = arr.map(value => (value - avg)**2)
return average(num)
}
/**
* Calculated standard deviation for an array of numbers
* @param {Array} values
*/
export function standardDeviation(arr){
return Math.sqrt(variance(arr))
}
/**
* Wraps a positive out of range index
* @param {number} index
* @param {number} length
*/
export function wrapIndex(index, length) {
return ((index % length) + length) % length
}