-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmypolyfill-Array1.js
More file actions
97 lines (66 loc) · 4.36 KB
/
mypolyfill-Array1.js
File metadata and controls
97 lines (66 loc) · 4.36 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// myArray = [1,2,2,[3,3,3,3],[1,2,[4,4,5]],6,[8,8,[9,8,9,9,9]],[[10,10,10]],[1,[3,3,3],2,2,1,1]]
const myArray = [1,2,2,[3,3,3,3],[1,2,[4,4,5]],6,[8,8,[9,8,9,9,9]],[[10,10,10]],[1,[3,3,3],2,2,1,1]];
// Polyfill #1 - Flatten N-Level Nested Array of Numbers
if(!Array.prototype.nlevelFlatten){
Array.prototype.nlevelFlatten = function(){
const flattenArr = (arr) => {
return arr.reduce((accum,val) => {
return Array.isArray(val) ? accum.concat(flattenArr(val)):accum.concat(val);
},[]);
};
return flattenArr(this).filter(e => typeof e === 'number' && !isNaN(e))
};
}
// let nlevelFlattenedArray = myArray.nlevelFlatten(); // Since nlevelFlatten is now a prototype of Array we can directly call it here.
// console.log(`My new flattend array:\n ${nlevelFlattenedArray}`);
// Polyfill #2 - Calculate Mean Value Of N-Level Nested Array Of Numbers Rounded to 6 Decimal Places
if (!Array.prototype.mean){
Array.prototype.mean = function (){
const myFlatArr = this.nlevelFlatten(); // Calling my own polyfill above nlevelFlatten to flatten if input array is not flat.
if (myFlatArr.length === 0) return NaN;
const sum = myFlatArr.reduce((accumulator, val) => accumulator + val,0);
return parseFloat((sum / myFlatArr.length).toFixed(6));
};
}
// console.log(`Mean Value Rounded to 6 Decimal Places Of N-Level Nested Array Of Numbers : ${myArray.mean()}`);
// Polyfill #3 - Calculate Median Value Of N-Level Nested Array Of Numbers Rounded to 6 Decimal Places
if (!Array.prototype.median){
Array.prototype.median = function(){
const myFlatArr = this.nlevelFlatten().slice().sort((a,b) => a-b); // Here , with slice() here we are shallow copying the flattened array so that it does not get corrupted and with sort() we are sorting in asecending order.
if(myFlatArr.length === 0) return NaN;
const middle = Math.floor(myFlatArr.length / 2); // Here we are trying to get the middle value of the distribution of the numbers in the array we got in myFlatArr array, because we are trying to calculate median.
return myFlatArr.length % 2 !== 0 ? myFlatArr[middle] : parseFloat(((myFlatArr[middle - 1] + myFlatArr[middle]) / 2).toFixed(6));
};
}
// console.log(`Median Value Rounded to 6 Decimal Places Of N-Level Nested Array Of Numbers : ${myArray.median()}`);
// Polyfill #4 - Calculate Mode Value Of N-Level Nested Array Of Numbers Rounded to 6 Decimal Places
if (!Array.prototype.mode){
Array.prototype.mode = function(){
const myFlatArr = this.nlevelFlatten();
if(myFlatArr.length === 0) return NaN;
const frequency = {}; // Creating an empty object to store the occurrence os different values or numbers in the flattend array.
myFlatArr.forEach(e => { // The forEach here iterates over the elements or numbers in the flattened array one by one
if (frequency[e]){ // if condition checks whether the current element in array is present as key in the frequency object or not if present ,if yes then value of that key is incremented by 1. If the current no. as key doesn't exist yet in frequency object , then the key is added and a count of 1 is assigned.
frequency[e] = frequency[e] + 1;
}
else{
frequency[e] = 1;
}
}); // At the end of this forEach loop the frequency object will look like this - frequency = {3 : 1, 1:2, 2:5, 4:1, 5:10, ....}
// const maxFrequency = Math.max(...Object.values(frequency));
let maximum = 0;
for (let key in frequency){ // this loop check every key in frequency object and updates the maximum if it finds a bigger count.
if (frequency[key] > maximum){
maximum = frequency[key];
}
}
const mode = [];
for (let key in frequency){ // this loop collects the keys that have the value equal to maximum
if(frequency[key]===maximum){
mode.push(Number(key));
}
}
return mode;
};
}
// console.log(`Mode Value / Values Of N-Level Nested Array Of Numbers : ${myArray.mode()}`);