Skip to content

Commit c19e4ec

Browse files
author
Tom Tarpey
committed
Done task 1 and 2
1 parent 1146fc5 commit c19e4ec

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

assignments/callbacks.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,64 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
22

33
function firstItem(arr, cb) {
44
// firstItem passes the first item of the given array to the callback function.
5+
return cb(arr[0]);
56
}
67

78
function getLength(arr, cb) {
89
// getLength passes the length of the array into the callback.
10+
return cb(arr.length);
911
}
1012

1113
function last(arr, cb) {
1214
// last passes the last item of the array into the callback.
15+
return cb(arr[arr.length - 1]);
1316
}
1417

1518
function sumNums(x, y, cb) {
1619
// sumNums adds two numbers (x, y) and passes the result to the callback.
20+
return cb(x + y);
1721
}
1822

1923
function multiplyNums(x, y, cb) {
2024
// multiplyNums multiplies two numbers and passes the result to the callback.
25+
return cb(x * y);
2126
}
2227

28+
// testing
29+
// -- 1 --
30+
firstItem(items, (item) => {
31+
console.log(item)
32+
}); // --> Pencil
33+
34+
// -- 2 --
35+
getLength(items, (length) => {
36+
console.log(length);
37+
}); // --> 4
38+
39+
// -- 3 --
40+
last(items, (item) => {
41+
console.log(item);
42+
}); // --> Gum
43+
44+
// -- 4 --
45+
sumNums(4, 6, (result) => {
46+
console.log(result);
47+
}); // --> 10
48+
49+
// -- 5 --
50+
multiplyNums(4, 6, (result) => {
51+
console.log(result);
52+
}); // --> 24
53+
54+
55+
56+
/* STRETCH PROBLEM */
57+
2358
function contains(item, list, cb) {
2459
// contains checks if an item is present inside of the given array/list.
2560
// Pass true to the callback if it is, otherwise pass false.
2661
}
2762

28-
/* STRETCH PROBLEM */
29-
3063
function removeDuplicates(array, cb) {
3164
// removeDuplicates removes all duplicate values from the given array.
3265
// Pass the duplicate free array to the callback function.

0 commit comments

Comments
 (0)