-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-map.js
More file actions
31 lines (28 loc) · 832 Bytes
/
array-map.js
File metadata and controls
31 lines (28 loc) · 832 Bytes
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
Array.prototype.customMap = function(callback) {
// callback - [Function: average]
// this - the array map() was called upon.
const newArray = [];
for (let i = 0; i < this.length; i++) {
newArray.push(callback(this[i], i, this));
}
return newArray;
};
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const positive = (num) => num > 0
const average = (num, idx, arr) => {
const prev = arr[idx - 1];
const next = arr[idx + 1];
let count = 1;
let total = num;
if (prev !== undefined) {
count++;
total += prev;
}
if (next !== undefined) {
count++;
total += next;
}
const average = total / count;
return Math.round(average * 100) / 100;
}
console.log(numbers.filter(positive).customMap(average)); // [2, 2.67, 2, 3.33, 5, 5.33, 5.67, 4]