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 pathq2.js
More file actions
59 lines (48 loc) · 1.23 KB
/
q2.js
File metadata and controls
59 lines (48 loc) · 1.23 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
/* Question 2
*
* Implement the functions defined below
*
*/
// Meant to be used by median. Do not alter.
const round = function(number) {
return Math.round(number * 100) / 100;
};
/* ===========================================================================
* MEDIAN - the middle number of a list (when sorted)
* - if the list length is even, then the median is the average of the two middle values
* - use the provided 'round' function before returning your value
*
* For example:
*
* median([6,2,3,4,9,6,1,0,5]);
*
* Returns:
*
* 4
*/
// 6,2,3,4,9,7
// 0 1 2 3 4 5
// length 6 = 6 / 2 = 3 - 1 = 2
// middleIndex 2 and 3
const median = function(arr) {
// sort the input arr
arr.sort();
// calculate the middle index
const middleIndex = (arr.length - 1) / 2;
// is the array even-length?
if (arr.length % 2 === 0) {
// even-length
const rightIndex = arr.length / 2;
const leftIndex = rightIndex - 1;
const rightVal = arr[rightIndex];
const leftVal = arr[leftIndex];
const avg = (rightVal + leftVal) / 2;
return round(avg);
} else {
// odd-length
const answer = arr[middleIndex];
return answer;
}
};
// Don't change below:
module.exports = { median };