-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy patharrays-callbacks.js
127 lines (85 loc) · 4.51 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
// ==== 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(animalName) {
return displayNames.push('Name: ' + animalName.animal_name + ', Scientific Name: ' + animalName.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(animalName2) {
return lowCaseAnimalNames.push(animalName2.animal_name + ', ');
});
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 = [];
let lessthan5 = zooAnimals.filter(less5 => (less5["population"] < 5));
lowPopulationAnimals.push(lessthan5);
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.
*/
let populationTotal = 0;
zooAnimals.reduce(function(accumalator,populations) {
populationTotal = accumalator + populations.population
return populationTotal;
},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!"
*/
const add = function(a, b) {
return a + b;
}
const multiply = function(a, b) {
return a * b;
}
const greeting = function(a, b) {
return `Hello ${a} ${b}, 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.
*/