-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.js
More file actions
129 lines (97 loc) · 3 KB
/
callbacks.js
File metadata and controls
129 lines (97 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-disable */
/* For portion of the assignment your job is to write functions
* so that each function invocation below works. You're working backwards.
*
* There are no tests for this file.
*
* Example:
*
* greeting('Hey guys', (message) => {
* console.log(message);
* });
*
* You would then define the greeting function to make the invocation work.
*
*
* const greeting = (str, cb) => {
* cb(str);
* };
*
*/
// Write a function called firstItem that passes the first item of the given array to the callback function
// code here
const firstItem = (foods, cb) => {
cb(foods[0]);
};
const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango'];
console.log(foods);
firstItem(foods, (firstItem) => {
console.log(`The first item is ${firstItem}.`);
});
// Write a function called getLength that passes the length of the array into the callback
// code here
const getLength = (foods, cb) => {
cb(foods.length)
}
getLength(foods, (length) => {
console.log(`The length of the array is ${length}.`);
});
// Write a function called last which passes the last item of the array into the callback
// code here
const last = (foods, cb) => {
cb(foods[foods.length - 1])
}
last(foods, (lastItem) => {
console.log(`The last item in the array is ${lastItem}.`);
});
// Write a function called sumNums that adds two numbers and passes the result to the callback
// code here
const sumNums = (x, y, cb) => {
cb(x+y);
}
sumNums(5, 10, (sum) => {
console.log(`The sum is ${sum}.`);
});
// Write a function called multiplyNums that multiplies two numbers and passes the result to the callback
// code here
const multiplyNums = (x, y, cb) => {
cb(x*y)
}
multiplyNums(5, 10, (product) => {
console.log(`The product is ${product}.`);
});
// Write a function called contains that checks if an item is present inside of the given array.
// Pass true to the callback if it is, otherwise pass false
// code here
const contains = (haystack, needle, cb) => {
return cb(haystack.indexOf(needle) >= 0);
}
contains(foods, 'ribeye', (result) => {
console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array');
});
// Write a function called removeDuplicates that removes all duplicate values from the given array.
// Pass the array to the callback function. Do not mutate the original array.
// code here
const removeDuplicates = (foods, cb) => {
const noDupes = [];
for(let i = 0; i < foods.length; i++) {
if(noDupes.indexOf(foods[i]) < 0) {
noDupes.push(foods[i])
}
}
cb(noDupes);
}
removeDuplicates(foods, (uniqueFoods) => {
console.log(`foods with duplicates removed: ${uniqueFoods}`);
});
// Write a function called forEach that iterates over the provided array and passes the value and index into the callback.
// code here
const forEach = (foods, cb) => {
for(let i = 0; i < foods.length; i++) {
cb(foods[i], i)
}
}
forEach(foods, (value, index) => {
console.log(`${value} is at index ${index}.`);
});
/* eslint-enable */