-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects-arrays.js
More file actions
293 lines (255 loc) · 7.41 KB
/
objects-arrays.js
File metadata and controls
293 lines (255 loc) · 7.41 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// ==== Objects ====
/*
Given the following information about dinosaurs, create 3 objects:
Use this pattern to create your objects:
object name, diet, weight, length, period
*/
class Dino {
constructor(name, diet, weight, length, period) {
this.name = name;
this.diet = diet;
this.weight = weight;
this.length = length;
this.period = period;
}
weighs() {
return `The ${this.name} weighed around ${this.weight}.`;
}
eats() {
return `The ${this.name} ate a ${this.diet} diet.`;
}
howLong() {
return `The ${this.name} was around ${this.length} long.`;
}
time() {
return `The ${this.name} lived in the ${this.period} period.`;
}
roar() {
return `The ${this.name} says RAWERSRARARWERSARARARRRR!`;
}
}
// tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceous
let dinosaur1 = new Dino(
"tyrannosaurus",
"carnivorous",
"7000kg",
"12m",
"Late Cretaceous"
);
// stegosaurus, herbivorous, 2000kg, 9m, Late Jurassic
let dinosaur2 = new Dino(
"stegosaurus",
"herbivorous",
"2000kg",
"9m",
"Late Jurassic"
);
// velociraptor, carnivorous, 15kg, 1.8m, Late Cretaceous
let dinosaur3 = new Dino(
"velociraptor",
"carnivorous",
"15kg",
"1.8m",
"Late Cretaceous"
);
// Using your dinosaur objects, log answers to these questions:
// How much did tyrannosaurus weigh?
console.log(dinosaur1.weighs());
// What was the diet of a velociraptor?
console.log(dinosaur3.eats());
// How long was a stegosaurus?
console.log(dinosaur2.howLong());
// What time period did tyrannosaurus live in?
console.log(dinosaur1.time());
// Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result.
console.log(dinosaur1.roar());
// ==== Arrays ====
// Given an array of college graduates. Complete the following requests using any array method you like
const graduates = [
{
id: 1,
first_name: "Cynde",
university: "Missouri Southern State College",
email: "ctorry0@macromedia.com"
},
{
id: 2,
first_name: "Saundra",
university: "The School of the Art Institute of Chicago",
email: "swhal1@state.gov"
},
{
id: 3,
first_name: "Lambert",
university: "Marian College",
email: "lparham2@techcrunch.com"
},
{
id: 4,
first_name: "Modestine",
university: "International Medical & Technological University",
email: "mdolder3@symantec.com"
},
{
id: 5,
first_name: "Chick",
university: "Sultan Salahuddin Abdul Aziz Shah Polytechnic",
email: "camorts4@google.com.au"
},
{
id: 6,
first_name: "Jakob",
university:
"Fachhochschule Rosenheim, Hochschule für Technik und Wirtschaft",
email: "jharken5@spiegel.de"
},
{
id: 7,
first_name: "Robbi",
university: "Salem University",
email: "rbrister6@redcross.org"
},
{
id: 8,
first_name: "Colline",
university: "Coastal Carolina University",
email: "cbrosh7@alibaba.com"
},
{
id: 9,
first_name: "Michail",
university: "Universidad Católica de Ávila",
email: "mrome8@shinystat.com"
},
{
id: 10,
first_name: "Hube",
university: "Universitat Rovira I Virgili Tarragona",
email: "hlethbrig9@foxnews.com"
}
];
/* Request 1: Create a new array called universities that contains all the universities in the graduates array.
Once you have the new array created, sort the universities alphabetically and log the result. */
const universities = graduates.map(({ university }) => university);
console.log(universities);
/* Request 2: Create a new array called contactInfo that contains both first name and email of each student.
The resulting contact information should have a space between the first name and the email information like this:
Name email@example.com
Log the result of your new array. */
const contactInfo = [];
graduates.forEach(bothNames);
function bothNames(graduates) {
contactInfo.push(graduates.first_name + " " + graduates.email);
}
console.log(contactInfo);
/* Request 3: Find out how many universities have the string "Uni" included in their name. Create a new array called uni that contains them all. Log the result. */
const uni = [];
for (var i = 0; i < graduates.length; i++) {
if (graduates[i].university.indexOf("Uni") > -1) {
uni.push(graduates[i].university);
}
}
console.log(uni);
// console.log(uni);
// ==== 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.
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 zoo wants to display both the scientific name and the animal name in front of the habitats. Return an array with only the animal and scientific names in it. The individual values in the array should look like this "Name: Jackal, asiatic, Scientific: Canis aureus."
*/
const animalNames = [];
zooAnimals.forEach(sciAni);
function sciAni(zooAnimals) {
animalNames.push(zooAnimals.scientific_name + " " + zooAnimals.animal_name);
}
console.log(animalNames);
/* Request 2: .map()
The zoos need a list of all their animal's names (names only, not scientific) converted to lower case. Create a new array named lowerCase and map over each name to convert them all to lower case. Log the resut.
*/
const lowerCase = zooAnimals.map((zooAnimal, index, zooAnimals) => {
return zooAnimal.animal_name.toLowerCase();
});
console.log(lowerCase);
/* Request 3: .filter()
The zoos are concenred about animals with a lower population count. Find out which animals have a population less than 5.
*/
const lowerPopulation = zooAnimals.filter(zooAnimal => {
if (zooAnimal.population <= 5) {
return true;
}
});
console.log(lowerPopulation);
/* 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.
*/
const populationTotal = zooAnimals.reduce(
(population, zooAnimal, index, zooAnimals) => {
return (population += zooAnimal.population);
},
0
);
console.log(populationTotal);
/*
Stretch: If you haven't already, convert your array method callbacks into arrow functions.
*/