forked from bloominstituteoftechnology/JavaScript-II
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.js
More file actions
188 lines (129 loc) · 4.09 KB
/
Copy pathcallbacks.js
File metadata and controls
188 lines (129 loc) · 4.09 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Create a higher order function and invoke the callback 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.
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
/*
//Given this problem:
function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
}
// Potential Solution:
// Higher order function using "cb" as the call back
function firstItem(arr, cb) {
return cb(arr[0]);
}
// Function invocation
firstItem(items, function(first) {
console.log(first)
}); */
/* function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(arr);
} */
/* version one */
/*
function length(arr) {
console.log(arr.length);
}
getLength(items, length); */
/* version two */
/* getLength(items, function length(arr) {
console.log(arr.length);
});
*/
function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(arr.length);
}
getLength(items, console.log);
/* ---------------------------------------------------------------- */
/* function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr]);
}
*/
/* version one */
/* function lastItem(arr) {
console.log(arr[arr.length -1]);
}
last(items, lastItem); */
/* version two */
/* last(items, function lastItem(arr) {
console.log(arr[arr.length -1]);
}); */
function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length -1]);
}
last(items, console.log);
/* ---------------------------------------------------------------- */
/* function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x, y);
} */
/* There are some problems with this version!
function sum(x,y){
return cb(x + y);
}
sumNums(2, 3, sum);
*/
/* version two */
/* sumNums(2, 3, function(x, y) {
console.log(x + y)
});
*/
function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x + y);
}
sumNums(2, 3, console.log);
/* ---------------------------------------------------------------- */
/* function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x, y);
} */
/* There are some problems with this version!
function multiply(x, y){
console.log(x * y);
}
multiplyNums(2, 3, multiply(x, y));
*/
/* version two */
/* multiplyNums(2, 3, function(x, y) {
console.log(x * y)
}); */
function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x * y);
}
multiplyNums(2, 3, console.log);
/* ---------------------------------------------------------------- */
/*
function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
return cb(item, list)
} */
/* version one */
/* function containCheck(item, list) {
console.log(list.includes(item));
}
contains("Pencil", items, containCheck); */
/* version two */
/* contains("Pencil", items, function(item, list) {
console.log(list.includes(item));
});
*/
function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
return cb(list.includes(item))
}
contains("Pencil", items, console.log);
/* STRETCH PROBLEM */
function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
return cb(array.filter((value, index, self) => self.indexOf(value) === index))
}
const itemsCheated = ['Notebook', 'Pencil','Gum', 'Pencil', 'Notebook', 'yo-yo', 'Gum', 'Notebook'];
removeDuplicates(itemsCheated, console.log);