-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayUtils.js
88 lines (72 loc) · 2.34 KB
/
arrayUtils.js
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
function forEach(arr, callback) {
if (arr instanceof Array) {
for (let i = 0; i < arr.length; i++) {
callback(arr[i], i, arr);
}
} else
throw new Error(`${arr} isn't Array`)
}
forEach([1, 2, 3], (num, i, nums) => console.log(`Number: ${num}, index: ${i},`, nums));
function map(arr, callback) {
if (arr instanceof Array) {
let results = [];
for (let i = 0; i < arr.length; i++) {
results.push(callback(arr[i], i, arr));
}
return results;
}
throw new Error(`${arr} isn't Array`)
}
let arr = map([1, 2, 3], (num) => num**2);
forEach(arr, (num, i, nums) => console.log(`Number: ${num}, index: ${i},`, nums));
function filter(arr, callback) {
if (arr instanceof Array) {
let results = [];
for (let i = 0; i < arr.length; i++) {
if (callback(arr[i], i, arr)) {
results.push(arr[i]);
}
}
return results;
}
throw new Error(`${arr} isn't Array`)
}
let data = filter(['Hello', ',', 'JavaScript', 'World', '!'], (str) => str.toLowerCase() !== str);
forEach(data, (num, i, nums) => console.log(`Number: ${num}, index: ${i},`, nums));
function reduce(arr, callback, startValue) {
if (arr instanceof Array) {
let result = startValue;
for (let i = 0; i < arr.length; i++) {
result = callback(result, arr[i], i, arr);
}
return result;
}
throw new Error(`${arr} isn't Array`)
}
let arr1 = reduce([1,2,3], (accumulator, currentValue) => accumulator + currentValue, 80);
console.log(arr1);
function every(arr, callback) {
if (arr instanceof Array) {
for (let i = 0; i < arr.length; i++) {
if (callback(arr[i], i, arr)) {
return true;
}
}
return false;
}
throw new Error(`${arr} isn't Array`)
}
console.log(every([10, 20, 30, 40, 50], (element) => element >= 10 ));
console.log(every([1, 2, 3, 0, 5], (element) => element >= 10 ));
function some(arr, callback) {
if (arr instanceof Array) {
for (let i = 0; i < arr.length; i++) {
if (callback(arr[i], i, arr)) {
return true;
}
}
return false;
}
throw new Error(`${arr} isn't Array`)
}
console.log(some([1, 2, 3, 4, 5], (element) => element % 2 === 0 ));