-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy patharrays-callbacks.js
145 lines (106 loc) · 5.14 KB
/
arrays-callbacks.js
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
// ==== ADVANCED Array Methods ====
// Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in the requests below to solve the problems.
const zooAnimals = [
{ animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" },
{ animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" },
{ animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" },
{ animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" },
{ animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" },
{ animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" },
{ animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" },
{ animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" },
{ animal_name: "Hawk-eagle, crowned", population: 10, scientific_name: "Spizaetus coronatus", state: "Florida" },
{ animal_name: "Australian pelican", population: 5, scientific_name: "Pelecanus conspicillatus", state: "West Virginia" },
];
/* Request 1: .forEach()
The zoos want to display both the scientific name and the animal name in front of the habitats. Populate the displayNames array with only the animal_name and scientific_name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus."
*/
const displayNames = [];
zooAnimals.forEach(function(currentValue){
displayNames.push(
`Name: ${currentValue.animal_name},
Scientific: ${currentValue.scientific_name}.`
);
});
console.log(displayNames);
/* Request 2: .map()
The zoos need a list of all their animal's names (animal_name only) converted to lower case. Using map, create a new array of strings named lowCaseAnimalNames, each string following this pattern: "jackal, asiatic". Log the resut.
*/
const lowCaseAnimalNames = [];
zooAnimals.map(function(currentValue){
lowCaseAnimalNames.push(
currentValue.animal_name.toLowerCase());
});
console.log(lowCaseAnimalNames);
/* Request 3: .filter()
The zoos are concerned about animals with a lower population count. Using filter, create a new array of objects called lowPopulationAnimals which contains only the animals with a population less than 5.
*/
const lowPopulationAnimals = zooAnimals.filter(
function(currentValue){
return currentValue.population < 5;
}
);
console.log(lowPopulationAnimals);
/* Request 4: .reduce()
The zoos need to know their total animal population across the United States. Find the total population from all the zoos using the .reduce() method. Remember the reduce method takes two arguments: a callback (which itself takes two args), and an initial value for the count.
*/
const populationTotal = zooAnimals.reduce(
function(callBackPopReduce, data){
return callBackPopReduce + data.population;
}, 0);
console.log(populationTotal);
// ==== Callbacks ====
/* Step 1: Create a higher-order function
* Create a higher-order function named consume with 3 parameters: a, b and cb
* The first two parameters can take any argument (we can pass any value as argument)
* The last parameter accepts a callback
* The consume function should return the invocation of cb, passing a and b into cb as arguments
*/
function consume (a, b, cb){
return cb(a, b);
}
/* Step 2: Create several functions to callback with consume();
* Create a function named add that returns the sum of two numbers
* Create a function named multiply that returns the product of two numbers
* Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!"
*/
function add(a, b){
return a + b;
}
function multiply(a, b){
return a * b;
}
function greeting(firstName, lastName){
return `Hello ${firstName} ${lastName}, nice to meet you!`;
}
/* Step 3: Check your work by un-commenting the following calls to consume(): */
console.log(consume(2, 2, add)); // 4
console.log(consume(10, 16, multiply)); // 160
console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you!
/*
Stretch: If you haven't already, convert your array method callbacks into arrow functions.
*/
const displayNames2 = [];
zooAnimals.forEach(currentValue => {
displayNames2.push(
`Name: ${currentValue.animal_name},
Scientific: ${currentValue.scientific_name}.`
);
});
const lowCaseAnimalNames2 = [];
zooAnimals.map(currentValue => {
lowCaseAnimalNames2.push(
currentValue.animal_name.toLowerCase());
});
const lowPopulationAnimals2 = zooAnimals.filter(
currentValue => {
return currentValue.population < 5;
});
const populationTotal2 = zooAnimals.reduce(
(callBackPopReduce, data) => {
return callBackPopReduce + data.population;
}, 0);
console.log(displayNames2);
console.log(lowCaseAnimalNames2);
console.log(lowPopulationAnimals2);
console.log(populationTotal2);