-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathcallbacks.js
132 lines (105 loc) · 3.25 KB
/
callbacks.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Challenge 1
function addTwo(num) {
return num + 2;
}
// To check if you've completed it, uncomment these console.logs!
// console.log(addTwo(3));
// console.log(addTwo(10));
// Challenge 2
function addS(word) {
return word + 's';
}
// uncomment these to check your work
// console.log(addS('pizza'));
// console.log(addS('bagel'));
// Challenge 3
function map(array, callback) {
const mappedArray = [];
for (let i = 0; i < array.length; i++) {
mappedArray.push(callback(array[i]));
}
return mappedArray;
}
// console.log(map([1, 2, 3], addTwo));
// Challenge 4
function forEach(array, callback) {
for (let i = 0; i < array.length; i++) {
callback(array[i]);
}
}
// see for yourself if your forEach works!
//--------------------------------------------------
// Extension
//--------------------------------------------------
//Extension 1
function mapWith(array, callback) {
const mappedArray = [];
forEach(array, el => mappedArray.push(callback(el)));
return mappedArray;
}
//Extension 2
function reduce(array, callback, initialValue) {
let acc = initialValue;
for (let i = 0; i < array.length; i++) {
acc = callback(acc, array[i]);
}
return acc;
}
//Extension 3
function intersection(arrays) {
return arrays.reduce((acc, curr) => {
return curr.filter(el => acc.includes(el));
});
}
// console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
// should log: [5, 15]
//Extension 4
function union(arrays) {
return arrays.reduce((acc, curr) => {
const newElements = curr.filter(el => !acc.includes(el));
return acc.concat(newElements);
});
}
// console.log(union([[5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5]]));
// should log: [5, 10, 15, 88, 1, 7, 100]
//Extension 5
function objOfMatches(array1, array2, callback) {
const matchObj = {};
for (let i = 0; i < array1.length; i++) {
if (callback(array1[i]) === array2[i]) {
matchObj[array1[i]] = array2[i];
}
}
return matchObj;
}
// console.log(objOfMatches(['hi', 'howdy', 'bye', 'later', 'hello'], ['HI', 'Howdy', 'BYE', 'LATER', 'hello'], function(str) { return str.toUpperCase(); }));
// should log: { hi: 'HI', bye: 'BYE', later: 'LATER' }
//Extension 6
function multiMap(arrVals, arrCallbacks) {
const multiMapObj = {};
for (let i = 0; i < arrVals.length; i++) {
multiMapObj[arrVals[i]] = [];
for (let j = 0; j < arrCallbacks.length; j++) {
multiMapObj[arrVals[i]].push(arrCallbacks[j](arrVals[i]));
}
}
return multiMapObj;
}
// console.log(multiMap(['catfood', 'glue', 'beer'], [function(str) { return str.toUpperCase(); }, function(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }, function(str) { return str + str; }]));
// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }
//Extension 7
function objectFilter(obj, callback) {
const newObj = {};
for (let key in obj) {
if (callback(key) === obj[key]) {
newObj[key] = callback(key);
}
}
return newObj;
}
// const cities = {
// London: 'LONDON',
// LA: 'Los Angeles',
// Paris: 'PARIS',
// };
// console.log(objectFilter(cities, city => city.toUpperCase())) // Should log { London: 'LONDON', Paris: 'PARIS'}