Skip to content

Commit 561afd6

Browse files
Task 3: callback.js completed
1 parent 1146fc5 commit 561afd6

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎assignments/callbacks.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,73 @@ 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
}
7+
const firstIndex = (arr) => {
8+
console.log(arr[0]);
9+
}
10+
11+
firstItem(items, firstIndex);
612

713
function getLength(arr, cb) {
814
// getLength passes the length of the array into the callback.
15+
return cb(arr);
16+
}
17+
18+
const arrayLength = (arr) => {
19+
console.log(arr.length);
920
}
21+
getLength(items, arrayLength);
22+
1023

1124
function last(arr, cb) {
1225
// last passes the last item of the array into the callback.
26+
return cb(arr);
27+
}
28+
const lastIndex = (arr) => {
29+
console.log(arr[arr.length -1]);
1330
}
1431

32+
last(items, lastIndex);
33+
1534
function sumNums(x, y, cb) {
1635
// sumNums adds two numbers (x, y) and passes the result to the callback.
36+
return cb(x, y);
37+
}
38+
const addNums = (num1, num2) => {
39+
console.log(num1 + num2);
1740
}
1841

42+
sumNums(3,4, addNums);
43+
1944
function multiplyNums(x, y, cb) {
2045
// multiplyNums multiplies two numbers and passes the result to the callback.
46+
return cb(x, y);
47+
}
48+
49+
const multiply = (num1, num2) => {
50+
console.log(num1 * num2);
2151
}
2252

53+
multiplyNums(3,4,multiply);
54+
2355
function contains(item, list, cb) {
2456
// contains checks if an item is present inside of the given array/list.
2557
// Pass true to the callback if it is, otherwise pass false.
58+
return cb(item, list);
2659
}
2760

61+
const checkContains = (item, list) =>{
62+
if(list.includes(item)){
63+
console.log(true);
64+
}else{
65+
console.log(false);
66+
}
67+
}
68+
69+
contains("Gum", items, checkContains);
70+
contains("stick", items, checkContains);
71+
2872
/* STRETCH PROBLEM */
2973

3074
function removeDuplicates(array, cb) {

0 commit comments

Comments
 (0)