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
Copy file name to clipboardExpand all lines: assignments/array-methods.js
+14-11Lines changed: 14 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
1
+
// A local community center is holding a fund rasising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
2
2
3
3
// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName.
// The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result
63
66
letallCaps=[];
67
+
allCaps=runners.map(function(currentValue){
68
+
returncurrentValue.first_name.toUpperCase();
69
+
})
64
70
console.log(allCaps);
65
71
66
72
// ==== Challenge 3: Use .filter() ====
67
73
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
// The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result
73
82
letticketPriceTotal=[];
74
-
console.log(ticketPriceTotal);
75
-
76
-
// ==== Challenge 5: Be Creative ====
77
-
// Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above.
// 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.
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.
2
2
3
3
constitems=['Pencil','Notebook','yo-yo','Gum'];
4
4
5
5
/*
6
-
7
6
//Given this problem:
8
7
9
8
function firstItem(arr, cb) {
10
9
// firstItem passes the first item of the given array to the callback function.
11
10
}
12
-
13
11
// Potential Solution:
14
-
15
-
// Higher order function using "cb" as the call back
16
12
function firstItem(arr, cb) {
17
13
return cb(arr[0]);
18
14
}
19
-
20
-
// Function invocation
21
15
firstItem(items, function(first) {
22
16
console.log(first)
23
17
});
24
-
25
18
*/
26
19
27
20
28
21
functiongetLength(arr,cb){
29
22
// getLength passes the length of the array into the callback.
23
+
returncb(arr.length);
30
24
}
31
25
32
26
functionlast(arr,cb){
33
27
// last passes the last item of the array into the callback.
28
+
returncb(arr[arr.length-1]);
34
29
}
35
30
36
31
functionsumNums(x,y,cb){
37
32
// sumNums adds two numbers (x, y) and passes the result to the callback.
33
+
returncb(x+y);
38
34
}
39
35
40
36
functionmultiplyNums(x,y,cb){
41
37
// multiplyNums multiplies two numbers and passes the result to the callback.
38
+
returncb(x*y);
42
39
}
43
40
44
41
functioncontains(item,list,cb){
45
42
// contains checks if an item is present inside of the given array/list.
46
43
// Pass true to the callback if it is, otherwise pass false.
44
+
letplaceHolder=[];
45
+
for(leti=0;i<list.length;i++){
46
+
if(item===list[i]){
47
+
placeHolder.push(list[i]);
48
+
}
49
+
}
50
+
if(!placeHolder[0]){
51
+
returncb(false);
52
+
}else{
53
+
returncb(true);
54
+
}
47
55
}
48
56
57
+
// --ES6--
58
+
// function contains(item, list, cb) {
59
+
// cb(list.includes(item));
60
+
// }
61
+
62
+
contains('Pencil',items,(cb)=>(console.log(cb)));
63
+
49
64
/* STRETCH PROBLEM */
50
65
51
66
functionremoveDuplicates(array,cb){
52
67
// removeDuplicates removes all duplicate values from the given array.
53
68
// Pass the duplicate free array to the callback function.
0 commit comments