Skip to content

Commit 51ae381

Browse files
committed
do the excercises in callbacks.js
1 parent 770541a commit 51ae381

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

assignments/callbacks.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,57 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2727

2828
function getLength(arr, cb) {
2929
// getLength passes the length of the array into the callback.
30+
return cb(arr.length);
3031
}
3132

33+
getLength(items, function(len){
34+
console.log(len)
35+
});
36+
37+
3238
function last(arr, cb) {
3339
// last passes the last item of the array into the callback.
40+
return cb(arr[arr.length - 1]);
3441
}
3542

43+
last(items, function(l){
44+
console.log(l)
45+
});
46+
3647
function sumNums(x, y, cb) {
3748
// sumNums adds two numbers (x, y) and passes the result to the callback.
49+
return cb(x+y);
3850
}
3951

52+
sumNums(2, 3, function(r){
53+
console.log(r)
54+
});
55+
4056
function multiplyNums(x, y, cb) {
4157
// multiplyNums multiplies two numbers and passes the result to the callback.
58+
return cb(x*y);
4259
}
4360

61+
multiplyNums(2, 3, function(r){
62+
console.log(r)
63+
});
64+
4465
function contains(item, list, cb) {
4566
// contains checks if an item is present inside of the given array/list.
4667
// Pass true to the callback if it is, otherwise pass false.
68+
let truthness = false;
69+
for (let i = 0; i < list.length; i++){
70+
if (list[i] === item){
71+
truthness = true;
72+
}
73+
}
74+
return cb(truthness);
4775
}
4876

77+
contains("yo-yo", items, function(r){
78+
console.log(r)
79+
});
80+
4981
/* STRETCH PROBLEM */
5082

5183
function removeDuplicates(array, cb) {

0 commit comments

Comments
 (0)