Skip to content

Commit 1a96cfa

Browse files
AlexAlex
authored andcommitted
made changes to callbacks.js
1 parent 1146fc5 commit 1a96cfa

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎assignments/callbacks.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,59 @@ 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);
56
}
67

8+
function first (items) {
9+
console.log(items[0]);
10+
}
11+
12+
firstItem(items, first);
13+
714
function getLength(arr, cb) {
815
// getLength passes the length of the array into the callback.
16+
return cb(arr);
17+
}
18+
19+
function length (items) {
20+
console.log(items.length);
921
}
1022

23+
getLength(items, length);
24+
1125
function last(arr, cb) {
1226
// last passes the last item of the array into the callback.
27+
return cb(arr);
28+
}
29+
30+
function lastItem (items) {
31+
console.log(items[items.length-1]);
1332
}
1433

34+
last(items, lastItem);
35+
1536
function sumNums(x, y, cb) {
1637
// sumNums adds two numbers (x, y) and passes the result to the callback.
38+
return cb(x, y);
39+
}
40+
41+
function addition(x, y) {
42+
console.log(x + y);
1743
}
1844

45+
sumNums(2, 5, addition);
46+
1947
function multiplyNums(x, y, cb) {
2048
// multiplyNums multiplies two numbers and passes the result to the callback.
49+
return cb(x, y);
2150
}
2251

52+
function multiplication(x, y) {
53+
console.log(x * y);
54+
}
55+
56+
multiplyNums(2, 5, multiplication);
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.

0 commit comments

Comments
 (0)