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
// 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.
58
-
letfullName=[];
57
+
// 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.
58
+
letfullName=[]
59
+
runners.forEach((name)=>{
60
+
fullName.push(name.first_name+" "+name.last_name)
61
+
})
59
62
console.log(fullName);
60
63
61
64
// ==== Challenge 2: Use .map() ====
62
65
// 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
-
letallCaps=[];
64
-
console.log(allCaps);
66
+
letallCaps=runners.map((name)=>{
67
+
returnname.first_name.toUpperCase()
68
+
});
69
+
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
68
-
letlargeShirts=[];
74
+
letlargeShirts=runners.filter((shirt)=>{
75
+
returnshirt.shirt_size==="L"
76
+
});
69
77
console.log(largeShirts);
70
78
71
79
// ==== Challenge 4: Use .reduce() ====
72
80
// 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
// 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.
78
88
79
89
// Problem 1
80
-
90
+
// list everyone's name, email, etc.
91
+
runners.forEach((element)=>{
92
+
console.log(element);
93
+
})
81
94
// Problem 2
95
+
// print out last name all lower case
96
+
letallLowerCaps=runners.map((name)=>{
97
+
returnname.last_name.toLowerCase()
98
+
})
99
+
console.log(allLowerCaps)
100
+
// Problem 3
101
+
// return list of people who works for the same company
Copy file name to clipboardExpand all lines: assignments/callbacks.js
+31-17Lines changed: 31 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,7 @@
1
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
-
functioncallback(value){
5
-
console.log(value)
6
-
}
4
+
7
5
/*
8
6
9
7
//Given this problem:
@@ -28,39 +26,55 @@ function getLength(arr, cb) {
28
26
// getLength passes the length of the array into the callback.
29
27
returncb(arr.length)
30
28
}
31
-
getLength(items,callback)
29
+
getLength(items,(arr1)=>{
30
+
console.log(arr1)
31
+
})
32
32
33
33
functionlast(arr,cb){
34
34
// last passes the last item of the array into the callback.
35
35
returncb(arr[3])
36
36
}
37
-
last(items,callback)
37
+
last(items,(lastItem)=>{
38
+
console.log(lastItem);
39
+
})
38
40
39
41
functionsumNums(x,y,cb){
40
42
// sumNums adds two numbers (x, y) and passes the result to the callback.
41
-
returncb(x+y)
43
+
returncb(x,y)
42
44
}
43
-
sumNums(2,5,callback)
45
+
console.log(sumNums(10,10,(x,y)=>{
46
+
returnx+y
47
+
}));
44
48
45
49
46
50
functionmultiplyNums(x,y,cb){
47
51
// multiplyNums multiplies two numbers and passes the result to the callback.
48
-
returncb(x*y)
52
+
returncb(x,y)
49
53
}
50
-
multiplyNums(5,5,callback)
54
+
console.log(multiplyNums(5,5,(x,y)=>{
55
+
returnx*y
56
+
}));
51
57
52
58
functioncontains(item,list,cb){
53
59
// contains checks if an item is present inside of the given array/list.
54
60
// Pass true to the callback if it is, otherwise pass false.
0 commit comments