01.
function
median(arr) {
02.
let mid = Math.floor(arr.length / 2);
03.
return
(arr.length % 2 == 0)
04.
? (arr[mid-1] + arr[mid]) / 2
05.
: arr[mid];
06.
}
07.
08.
Array.prototype.fiveNumSummary =
function
() {
09.
this
.sort(
function
(a, b) {
return
a - b} );
10.
let mid = Math.floor(
this
.length / 2),
11.
loQ = (
this
.length % 2 == 0)
12.
?
this
.slice(0, mid)
13.
:
this
.slice(0, mid+1),
14.
hiQ =
this
.slice(mid);
15.
return
[
this
[0],
16.
median(loQ),
17.
median(
this
),
18.
median(hiQ),
19.
this
[
this
.length-1]
20.
];
21.
}
22.
23.
24.
let verification1 = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43],
25.
verification2 = [
26.
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
27.
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
28.
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
29.
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578
30.
],
31.
oneToTwenty = [
32.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
33.
],
34.
populationOfSwissCantons = [
35.
694_072, 16_293, 55_309, 1_043_132, 292_955, 201_156, 325_496, 506_343,
36.
40_851, 200_096, 73_709, 416_347, 175_894, 43_520, 38_108, 514_504,
37.
83_107, 277_462, 162_157, 282_909, 350_986, 36_819, 814_762, 348_503,
38.
128_794, 1_553_423
39.
];
40.
console.log( verification1.fiveNumSummary() );
41.
console.log( verification2.fiveNumSummary() );
42.
console.log( oneToTwenty.fiveNumSummary() );
43.
console.log( populationOfSwissCantons.fiveNumSummary() );