You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b070630
Browse filesBrowse the repository at this point in the historyBrowse files
// firstItem passes the first item of the given array to the callback function.
5
-
6
-
cb(arr[0]);
7
-
8
-
}
9
-
10
-
firstItem(item,function(Item){
11
-
12
-
console.log(`The first item is ${Item}`);
13
-
14
-
})
15
-
1
+
// 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.
16
2
3
+
constitems=['Pencil','Notebook','yo-yo','Gum'];
4
+
/*
5
+
//Given this problem:
6
+
7
+
function firstItem(arr, cb) {
8
+
// firstItem passes the first item of the given array to the callback function.
9
+
}
10
+
// Potential Solution:
11
+
function firstItem(arr, cb) {
12
+
return cb(arr[0]);
13
+
}
14
+
firstItem(items, function(first) {
15
+
console.log(first)
16
+
});
17
+
*/
17
18
18
19
functiongetLength(arr,cb){
19
-
20
20
// getLength passes the length of the array into the callback.
21
-
22
-
cb(arr.length);
23
-
21
+
returncb(arr.length);
24
22
}
25
23
26
-
getLength(items,function(length){
27
-
28
-
console.log(`The length of the array is ${length}.`)
29
-
30
-
})
31
-
24
+
getLength(items,function(length){
25
+
console.log(length);
26
+
});
32
27
33
28
34
29
functionlast(arr,cb){
35
-
36
30
// last passes the last item of the array into the callback.
37
-
38
-
cb(arr[arr.length-1]);
39
-
31
+
returncb(arr[arr.length-1]);
40
32
}
41
33
42
-
last(items,function(item){
43
-
44
-
console.log(`The last item of the array is ${item}`)
45
-
46
-
})
47
-
34
+
last(items,function(last){
35
+
console.log(last);
36
+
});
48
37
49
38
50
39
functionsumNums(x,y,cb){
51
-
52
40
// sumNums adds two numbers (x, y) and passes the result to the callback.
53
-
54
-
cb(x,y);
55
-
41
+
constsum=x+y;
42
+
returncb(sum);
56
43
}
57
44
58
-
sumNums(2,4,function(x,y){
59
-
60
-
console.log(`The result is`,x+y);
61
-
62
-
})
63
-
45
+
sumNums(3,6,function(sum){
46
+
console.log(sum);
47
+
});
64
48
65
49
66
50
functionmultiplyNums(x,y,cb){
67
-
68
51
// multiplyNums multiplies two numbers and passes the result to the callback.
69
-
70
-
cb(x,y)
71
-
52
+
constproduct=x*y;
53
+
returncb(product);
72
54
}
73
55
74
-
// multiplyNums(2,4,function(x , y){
75
-
76
-
// console.log(`The result is ` x * y);
77
-
78
-
// })
79
-
56
+
multiplyNums(3,6,function(product){
57
+
console.log(product);
58
+
});
80
59
81
60
82
61
functioncontains(item,list,cb){
83
-
84
62
// contains checks if an item is present inside of the given array/list.
85
-
86
63
// Pass true to the callback if it is, otherwise pass false.
64
+
if(list.includes(item)==true){
65
+
returncb(true);
66
+
}else{
67
+
returncb(false);
68
+
}
69
+
}
87
70
88
-
cb(item,list);
89
-
71
+
contains('Notebook',items,function(included){
72
+
console.log(included);
73
+
});
74
+
75
+
/* STRETCH PROBLEM */
76
+
77
+
functionremoveDuplicates(array,cb){
78
+
// removeDuplicates removes all duplicate values from the given array.
79
+
// Pass the duplicate free array to the callback function.
0 commit comments