-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbackAndHigherOrder.js
316 lines (229 loc) · 7.35 KB
/
callbackAndHigherOrder.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//////// PROBLEM 1 ////////
/*
Write a function called multiply that takes in three parameters: two numbers and a callback function.
Invoke the callback, passing in the product of the two numbers multiplied as the argument.
*/
// CODE HERE
const multiply = (num1, num2, callBack) => {
return callBack(num1 * num2)
};
// UNCOMMENT THE FUNCTION CALL BELOW
// RUN THIS FILE WITH NODE
// CHECK YOUR ANSWER
multiply(4, 3, answer => {
console.log('The answer is ' + answer) //should console.log 12
})
////////// PROBLEMS 2 - 6 //////////
// The names array will be used in problems 2 - 6.
// Do not edit the code below.
var names = ['Tyler', 'Cahlan', 'Ryan', 'Colt', 'Tyler', 'Blaine', 'Cahlan']
// Do not edit the code above.
////////// PROBLEM 2 //////////
/*
Write a function called first that takes in two parameters, an array and a callback function.
Then invoke the callback function, passing in the first element in the array as it's argument.
*/
// CODE HERE
const first = (arr, callBack) => {callBack(arr[0]);}
// UNCOMMENT THE FUNCTION CALL BELOW
// RUN THIS FILE WITH NODE
// CHECK YOUR ANSWER
first(names, firstName => {
console.log('The first name in names is ' + firstName)
})
////////// PROBLEM 3 //////////
/*
Write a function called last that takes in an array and a callback function.
Then invoke the callback, passing in the last element in the array as the argument.
*/
// CODE HERE
// const last = (arr, callBack) => {
// return callBack(arr[6]);
// }
const last = (arr, callBack) => {
return callBack(arr[arr.length - 1]);
}
// UNCOMMENT THE FUNCTION CALL BELOW
// RUN THIS FILE WITH NODE
// CHECK YOUR ANSWER
last(names, lastName => {
console.log('The last name in names is ' + lastName)
})
////////// PROBLEM 4 //////////
/*
Write a function called contains that takes in three parameters: an array, a name and a callback.
Check if the name exists in the array.
If it does, invoke the callback with true as the argument.
If the name does not exist, invoke the callback with false as the argument.
*/
// CODE HERE
const contains = (arr, name, callback) => {
if (arr.includes(name)) {
return callback(true);
} else {
return callback(false);
}
}
// UNCOMMENT THE FUNCTION CALL BELOW
// RUN THIS FILE WITH NODE
// CHECK YOUR ANSWER
contains(names, 'Colt', result => {
if (result === true) {
console.log('Colt is in the array')
} else {
console.log('Colt is not in the array')
}
})
////////// PROBLEM 5 //////////
/*
Write a function called uniq that takes in an array and a callback function.
Remove any duplicate values from the array, and invoke the callback with the modified array as an argument.
Hint: you can use a nested for loop to do this.
*/
// CODE HERE
const uniq = (arr, callBack) => {
// let unique = new Set(arr);
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1);
j--;
}
}
}
callBack(arr);
}
//Different solutions below
const unique = (arr, cb) => {
// let mySet = new Set();
// let arrSet = mySet.add(arr)
// let mySpread = [...mySet];
// let uniqueArray = [...new Set(arr)];
// let uniqueArray = arr.filter((name, index) => {
// return arr.indexOf(name) == index;
// });
cb(uniqueArray);
};
/*
Invoke the uniq function, passing in the names array from above and a callback function.
The callback function should take in one parameter called uniqArr.
The callback should print a string that says:
'The new names array with all the duplicate items removed is [UNIQARRPARAM].'
*/
// CODE HERE
uniq(names, (uniqArr) => {
console.log(`The new names array with all the duplicate items removed is ${uniqArr}`)
})
////////// PROBLEM 6 //////////
/*
Write a function called each that takes in an array of names and a callback function.
For each name in the array, invoke the callback and pass in the name and the name's index as arguments.
*/
// CODE HERE
const each = (arr, callBack) => {
arr.forEach(function (element, index) {
return callBack(element, index);
});
}
/*
Invoke the each function, passing in the names array and a callback function.
The callback function should take in two parameters, item and index.
The callback should print a string that says:
'The item at index [INDEXPARAM] is [ITEMPARAM].'
*/
// CODE HERE
each(names, (item, index) => {
console.log(`The item at index ${index} is ${item} `);
})
////////// PROBLEM 7 //////////
/*
Write a function called getUserById that takes in three parameters: an array of objects (users), an id and a callback, and searches for the user with a matching id.
When the correct user object is found, invoke the callback with the user object as an argument.
*/
// Do not edit the code below.
var users = [{
id: '12d',
email: 'tyler@gmail.com',
name: 'Tyler',
address: '167 East 500 North'
},
{
id: '15a',
email: 'cahlan@gmail.com',
name: 'Cahlan',
address: '135 East 320 North'
},
{
id: '16t',
email: 'ryan@gmail.com',
name: 'Ryan',
address: '192 East 32 North'
},
]
// Do not edit the code above.
// CODE HERE
const getUserById = (arr, id, callBack) => {
for (let i = 0; i < arr.length; i++) {
if (arr[i].id === id) {
return callBack(arr[i])
}
}
}
// UNCOMMENT THE FUNCTION CALL BELOW
// RUN THIS FILE WITH NODE
// CHECK YOUR ANSWER
getUserById(users, '16t', user => {
console.log('The user with the id 16t has the email of ' + user.email + ' the name of ' + user.name + ' and the address of ' + user.address)
})
////////// CHALLENGE //////////
/*
You'll be writing a higher order function that returns
another function.
Create a function called addingFactory that takes in
one parameter (it will be a number).
addingFactory should return a function that takes in
one parameter (this will be another number).
The (inner) function that's being returned should add
the two parameters together and return the sum.
*/
// CODE HERE
const addingFactory = x => y => x + y
const addingFactory = (x) => (y) => x + y;
const addingFactory = x => {
return function(y) {
return x + y
}
}
/*
Now that you have addingFactory, you can create other
functions from it.
You're going to invoke addingFactory and save the result
to a new variable.
Create a variable called addTen and set it equal to
the invocation of addingFactory passing in the number
10 as an arguemnt.
*/
// CODE HERE
const addTen = addingFactory(10)
/*
Now the inner function is stored in the addTen variable!
Call the addTen function passing in another number and
console log the result.
Call it a second time, passing in a different number
to see the different outputs.
*/
// CODE HERE
console.log(addTen(333))
console.log(addTen(737373))
/*
Let's make another function from the addingFactory.
This time, pass in your favorite number and name
the variable addNUMBER with the name of the number
you chose.
Once you create that, you can invoke the function
to add any number to your favorite number!
*/
// CODE HERE
const addSeven = addingFactory(7)
console.log(addSeven(7))
console.log(addSeven(777))