forked from ChristianNally/web-2023-Feb-06-west-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq4.js
More file actions
78 lines (63 loc) · 2.15 KB
/
q4.js
File metadata and controls
78 lines (63 loc) · 2.15 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
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
/* Question 4
*
* Implement the 'stdev' function defined below
*
* STDEV - the square root of the average of the squared deviations of the values from their average value
* - The formula is:
*
* stdev = sqrt(sum((x - populationMean)^2)/numberOfValues)
* volume = length * width * height
*
* - you are allowed to look at Wikipedia's example calculation to help you understand the formula
* - Keep in mind, we are using 'Population Standard Deviation' as opposed to 'Sample Standard Deviation' for this test
* https://en.wikipedia.org/wiki/Standard_deviation#Population_standard_deviation_of_grades_of_eight_students
*
* - use the provided 'round' function before returning your final value
* - you can take a square root using `Math.sqrt(number)`
*
* For example:
*
* stdev([6,2,3,4,9,6,1,0,5]);
*
* Returns:
*
* 2.67
*/
// This function is to be used by stdev. Do not alter.
const round = function(number) {
return Math.round(number * 100) / 100;
};
const q0Funcs = require('./q0');
const sum = q0Funcs.sum;
const stdev = function(arr) {
// const answer = sqrt(sum((element - populationMean)^2)/numberOfValues)
const numberOfValues = arr.length;
const populationMean = sum(arr) / numberOfValues;
// const differences = [];
// for (const element of arr) {
// const difference = element - populationMean;
// differences.push(difference);
// }
const differences = arr.map((element) => {
return element - populationMean;
});
// console.log(arr);
// console.log(populationMean);
// console.log(differences);
// const squares = [];
// for (const difference of differences) {
// const square = Math.pow(difference, 2);
// squares.push(square);
// }
const squares = differences.map((difference) => {
return Math.pow(difference, 2);
});
// console.log(squares);
// const answer = sqrt(sum((element - populationMean)^2)/numberOfValues)
const squareSum = sum(squares);
const squareSumAvg = squareSum / numberOfValues;
const answer = Math.sqrt(squareSumAvg);
return round(answer);
};
// Don't change below:
module.exports = { stdev };