Skip to content

Commit f2018ab

Browse files
committed
complete challenge on callback
1 parent 24ee4da commit f2018ab

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

assignments/array-methods.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5757
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName.
5858
let fullName = [];
5959
runners.forEach(function (runner) {
60-
fullName.push(`fist_name: ${runner.first_name}, last_name: ${runner.last_name} `)
60+
fullName.push(`${runner.first_name} ${runner.last_name} `)
6161
});
6262
console.log(fullName);
6363

assignments/callbacks.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.
22

3-
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
3+
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'Gum'];
44

55
/*
66
@@ -24,32 +24,51 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2424
2525
*/
2626

27+
// Function invocation
28+
const cb = (first) => {
29+
console.log(first)
30+
};
31+
32+
2733

2834
function getLength(arr, cb) {
29-
// getLength passes the length of the array into the callback.
35+
return cb(arr.length);
3036
}
37+
getLength(items, cb);
3138

3239
function last(arr, cb) {
33-
// last passes the last item of the array into the callback.
40+
return cb(arr[arr.length - 1]);
3441
}
42+
last(items,cb);
3543

3644
function sumNums(x, y, cb) {
3745
// sumNums adds two numbers (x, y) and passes the result to the callback.
46+
return cb(x + y);
3847
}
48+
sumNums(11,25, cb);
3949

4050
function multiplyNums(x, y, cb) {
4151
// multiplyNums multiplies two numbers and passes the result to the callback.
52+
return cb(x * y);
4253
}
4354

55+
multiplyNums(11, 25, cb);
56+
4457
function contains(item, list, cb) {
4558
// contains checks if an item is present inside of the given array/list.
4659
// Pass true to the callback if it is, otherwise pass false.
60+
cb(list.includes(item));
4761
}
4862

63+
contains("Notebook", items, cb);
4964
/* STRETCH PROBLEM */
5065

5166
function removeDuplicates(array, cb) {
5267
// removeDuplicates removes all duplicate values from the given array.
5368
// Pass the duplicate free array to the callback function.
5469
// Do not mutate the original array.
70+
cb(array.filter((item, index) => {
71+
return array.indexOf(item) >= index;
72+
}))
5573
}
74+
removeDuplicates(items, cb);

0 commit comments

Comments
 (0)