Skip to content

Commit 549aa6a

Browse files
committed
JS
Callbacks done
1 parent 91ff21a commit 549aa6a

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎assignments/callbacks.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
1+
//JS - II
12
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
23

34
function firstItem(arr, cb) {
45
// firstItem passes the first item of the given array to the callback function.
6+
cb(arr[0]);
57
}
68

79
function getLength(arr, cb) {
810
// getLength passes the length of the array into the callback.
11+
cb(arr.length);
912
}
1013

1114
function last(arr, cb) {
1215
// last passes the last item of the array into the callback.
16+
cb(arr[arr.length - 1]);
1317
}
1418

1519
function sumNums(x, y, cb) {
1620
// sumNums adds two numbers (x, y) and passes the result to the callback.
21+
let sum = x + y;
22+
cb(sum);
1723
}
1824

1925
function multiplyNums(x, y, cb) {
2026
// multiplyNums multiplies two numbers and passes the result to the callback.
27+
let product = x * y;
28+
cb(product);
2129
}
2230

2331
function contains(item, list, cb) {
2432
// contains checks if an item is present inside of the given array/list.
2533
// Pass true to the callback if it is, otherwise pass false.
34+
if(list.includes(item) === true) {
35+
cb(true);
36+
} else {
37+
cb(false);
38+
}
2639
}
2740

2841
/* STRETCH PROBLEM */

0 commit comments

Comments
 (0)