Skip to content

Commit b070630

Browse files
committed
Spent quite some time creating/updating callback file but finally got it to work
1 parent 2280094 commit b070630

1 file changed

Lines changed: 61 additions & 59 deletions

File tree

‎assignments/callbacks.js‎

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,92 @@
1-
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2-
function firstItem(arr, cb) {
3-
4-
// firstItem passes the first item of the given array to the callback function.
5-
6-
cb(arr[0]);
7-
8-
}
9-
10-
firstItem(item, function(Item){
11-
12-
console.log(`The first item is ${Item}`);
13-
14-
})
15-
1+
// Create a callback function and invoke the 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.
162

3+
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
4+
/*
5+
//Given this problem:
6+
7+
function firstItem(arr, cb) {
8+
// firstItem passes the first item of the given array to the callback function.
9+
}
10+
// Potential Solution:
11+
function firstItem(arr, cb) {
12+
return cb(arr[0]);
13+
}
14+
firstItem(items, function(first) {
15+
console.log(first)
16+
});
17+
*/
1718

1819
function getLength(arr, cb) {
19-
2020
// getLength passes the length of the array into the callback.
21-
22-
cb(arr.length);
23-
21+
return cb(arr.length);
2422
}
2523

26-
getLength(items, function(length){
27-
28-
console.log(`The length of the array is ${length}.`)
29-
30-
})
31-
24+
getLength(items, function(length) {
25+
console.log(length);
26+
});
3227

3328

3429
function last(arr, cb) {
35-
3630
// last passes the last item of the array into the callback.
37-
38-
cb(arr[arr.length -1 ]);
39-
31+
return cb(arr[arr.length-1]);
4032
}
4133

42-
last(items, function(item){
43-
44-
console.log(`The last item of the array is ${item}`)
45-
46-
})
47-
34+
last(items, function(last) {
35+
console.log(last);
36+
});
4837

4938

5039
function sumNums(x, y, cb) {
51-
5240
// sumNums adds two numbers (x, y) and passes the result to the callback.
53-
54-
cb(x,y);
55-
41+
const sum = x+y;
42+
return cb(sum);
5643
}
5744

58-
sumNums(2,4,function(x,y){
59-
60-
console.log(`The result is`, x + y);
61-
62-
})
63-
45+
sumNums(3, 6, function(sum) {
46+
console.log(sum);
47+
});
6448

6549

6650
function multiplyNums(x, y, cb) {
67-
6851
// multiplyNums multiplies two numbers and passes the result to the callback.
69-
70-
cb(x,y)
71-
52+
const product = x*y;
53+
return cb(product);
7254
}
7355

74-
// multiplyNums(2,4,function(x , y){
75-
76-
// console.log(`The result is ` x * y);
77-
78-
// })
79-
56+
multiplyNums(3, 6, function(product) {
57+
console.log(product);
58+
});
8059

8160

8261
function contains(item, list, cb) {
83-
8462
// contains checks if an item is present inside of the given array/list.
85-
8663
// Pass true to the callback if it is, otherwise pass false.
64+
if (list.includes(item) == true) {
65+
return cb(true);
66+
} else {
67+
return cb(false);
68+
}
69+
}
8770

88-
cb(item,list);
89-
71+
contains('Notebook', items, function(included) {
72+
console.log(included);
73+
});
74+
75+
/* STRETCH PROBLEM */
76+
77+
function removeDuplicates(array, cb) {
78+
// removeDuplicates removes all duplicate values from the given array.
79+
// Pass the duplicate free array to the callback function.
80+
// Do not mutate the original array.
81+
let newArr = [];
82+
for (let i=0; i<array.length; i++) {
83+
if (!newArr.includes(array[i])) {
84+
newArr.push(array[i]);
85+
}
86+
}
87+
return cb(newArr);
9088
}
89+
90+
removeDuplicates(items, function(items) {
91+
console.log(items);
92+
});

0 commit comments

Comments
 (0)