Skip to content

Commit 087d95a

Browse files
committed
Completed closure.js
1 parent ab36648 commit 087d95a

2 files changed

Lines changed: 58 additions & 58 deletions

File tree

‎assignments/callbacks.js‎

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
11
// 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.
22

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

5+
// // function firstItem(arr, cb) {
6+
// // // firstItem passes the first item of the given array to the callback function.
7+
// // }
8+
// // Potential Solution:
59
// function firstItem(arr, cb) {
6-
// // firstItem passes the first item of the given array to the callback function.
10+
// cb(arr[0]);
711
// }
8-
// Potential Solution:
9-
function firstItem(arr, cb) {
10-
cb(arr[0]);
11-
}
1212

13-
firstItem(items, (first) => {
14-
console.log(first);
15-
});
13+
// firstItem(items, (first) => {
14+
// console.log(first);
15+
// });
1616

17-
function getLength(arr, cb) {
18-
// // getLength passes the length of the array into the callback.
19-
console.log(cb(arr));
20-
}
21-
22-
23-
let arrayLength = function(items) {
24-
return items.length;
25-
}
17+
// function getLength(arr, cb) {
18+
// // // getLength passes the length of the array into the callback.
19+
// console.log(cb(arr));
20+
// }
2621

27-
getLength(items, arrayLength);
2822

29-
function last(arr, cb) {
30-
// last passes the last item of the array into the callback.
31-
cb(arr[arr.length - 1]);
32-
}
23+
// let arrayLength = function(items) {
24+
// return items.length;
25+
// }
3326

34-
last(items, (lastItem) => {
35-
console.log(lastItem);
36-
});
27+
// getLength(items, arrayLength);
3728

29+
// function last(arr, cb) {
30+
// // last passes the last item of the array into the callback.
31+
// cb(arr[arr.length - 1]);
32+
// }
3833

34+
// last(items, (lastItem) => {
35+
// console.log(lastItem);
36+
// });
3937

40-
function sumNums(x, y, cb) {
41-
// // sumNums adds two numbers (x, y) and passes the result to the callback.
42-
cb(x + y);
43-
}
4438

4539

46-
sumNums(10, 20, (callback) => {
47-
console.log(callback);
48-
});
40+
// function sumNums(x, y, cb) {
41+
// // // sumNums adds two numbers (x, y) and passes the result to the callback.
42+
// cb(x + y);
43+
// }
4944

5045

51-
function multiplyNums(x, y, cb) {
52-
// multiplyNums multiplies two numbers and passes the result to the callback.
53-
cb(x * y);
54-
}
46+
// sumNums(10, 20, (callback) => {
47+
// console.log(callback);
48+
// });
5549

56-
multiplyNums(10, 15, function(total) {
57-
console.log(total);
58-
});
5950

51+
// function multiplyNums(x, y, cb) {
52+
// // multiplyNums multiplies two numbers and passes the result to the callback.
53+
// cb(x * y);
54+
// }
6055

61-
function contains(item, list, cb) {
62-
// contains checks if an item is present inside of the given array/list.
63-
// Pass true to the callback if it is, otherwise pass false.
64-
for (let i = 0; i < list.length; i++) {
65-
if (list[i] === item) {
66-
return cb(true);
67-
} else {
68-
return cb(false);
69-
}
70-
}
71-
}
56+
// multiplyNums(10, 15, function(total) {
57+
// console.log(total);
58+
// });
59+
60+
61+
// function contains(item, list, cb) {
62+
// // contains checks if an item is present inside of the given array/list.
63+
// // Pass true to the callback if it is, otherwise pass false.
64+
// for (let i = 0; i < list.length; i++) {
65+
// if (list[i] === item) {
66+
// return cb(true);
67+
// } else {
68+
// return cb(false);
69+
// }
70+
// }
71+
// }
7272

7373

7474

75-
// /* STRETCH PROBLEM */
75+
// // /* STRETCH PROBLEM */
7676

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-
// }
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+
// // }
8282

8383

‎assignments/closure.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020

21-
// // ==== Challenge 2: Create a counter function ====
21+
// // // ==== Challenge 2: Create a counter function ====
2222
// const counter = () => {
2323
// for (let i = 0; i < counter.length; i++) {
2424
// return counter;

0 commit comments

Comments
 (0)