forked from epoch/javascript-basics-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverages.js
More file actions
53 lines (41 loc) · 1.46 KB
/
averages.js
File metadata and controls
53 lines (41 loc) · 1.46 KB
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
let someArray = [1, 2, 2, 2, 3, 3, 4, 5, 6]
function total(someArray) {
let thisTotal = someArray.reduce(function (total, num) {
return total + num
})
return thisTotal
}
function mean(someArray) { return total(someArray) / someArray.length }
function median(someArray) {
//the middle number by count, or the avg of the two centermost numbers
if (someArray.length % 2 === 0) {
index1 = (someArray.length / 2)
index2 = index1 + 1
return (someArray[index1] + someArray[index2]) / 2
} else {
let index = Math.floor(someArray.length / 2)
return someArray[index]
}
}
function mode(someArray) {
//the value which occurs most often in an array. a set with mutliple modes is multimodal
let kvPairs = someArray.reduce(function(obj, num){
// console.log(obj, num)
if (!obj[num]) {
obj[num] = 0
}
obj[num]++
return obj
}, {})
return kvPairs
}
console.log(total(someArray))
console.log(mean(someArray))
console.log(median(someArray))
console.log(mode(someArray))
// Write a function for each of the following. Each function will take an Array as the one argument..
// total - returns the total of all the values in the array
// mean - returns the mean (average) of the array
// median - returns the median of the array
// mode- returns an object representing the mode(s) of the array with the property being the mode and the value being the frequency
module.exports = {total: total, mean: mean, median: median, mode: mode}