-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects-arrays.js
More file actions
131 lines (111 loc) · 5.82 KB
/
objects-arrays.js
File metadata and controls
131 lines (111 loc) · 5.82 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
/// ==== Objects ====
/*
Given the following information about dinosaurs, create 3 objects:
Use this pattern to create your objects:
object name, diet, weight, length, period
*/
function Dinosaur(attrs) {
this.name = attrs.name;
this.diet = attrs.diet;
this.weight = attrs.weight;
this.length = attrs.length;
this.period = attrs.period;
}
// tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceious
const tyrannosaurus = new Dinosaur({
'name': 'tyrannosaurus',
'diet': 'carnivorous',
'weight': '7000kg',
'length': '12m',
'period': 'Late Cretaceious'
});
Dinosaur.prototype.roar = function() {
return(`RAWERSRARARWERSARARARRRR!`);
}
// stegosaurus, herbivorous, 2000kg, 9m, Late Jurassic
const stegosaurus = new Dinosaur({
'name': 'stegosaurus',
'diet': 'herbivorous',
'weight': '2000kg',
'length': '9m',
'period': 'Late Jurassic'
});
// velociraptor, carnivorous, 15kg, 1.8m, Late Cretaceious
const velociraptor = new Dinosaur({
'name': 'velociraptor',
'diet': 'carnivorous',
'weight': '15kg',
'length': '1.8m',
'period': 'Late Cretaceious'
});
// Using your dinosaur objects, log answers to these questions:
// How much did tyrannosaurus weigh?
console.log(tyrannosaurus.weight);
// What was the diet of a velociraptor?
console.log(velociraptor.diet);
// How long was a stegosaurus?
console.log(stegosaurus.length);
// What time period did tyrannosaurus live in?
console.log(tyrannosaurus.period);
// Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result.
console.log(tyrannosaurus.roar());
// ==== Arrays ====
// Given an array of college graduates. Complete the following requests without using any array methods like .forEach(), .map(), .reduce(), .filter()
const graduates = [{"id":1,"first_name":"Cynde","university":"Missouri Southern State College","email":"[email protected]"},
{"id":2,"first_name":"Saundra","university":"The School of the Art Institute of Chicago","email":"[email protected]"},
{"id":4,"first_name":"Modestine","university":"International Medical & Technological University","email":"[email protected]"},
{"id":5,"first_name":"Chick","university":"Sultan Salahuddin Abdul Aziz Shah Polytechnic","email":"[email protected]"},
{"id":6,"first_name":"Jakob","university":"Fachhochschule Rosenheim, Hochschule für Technik und Wirtschaft","email":"[email protected]"},
{"id":8,"first_name":"Colline","university":"Coastal Carolina University","email":"[email protected]"},
{"id":9,"first_name":"Michail","university":"Universidad Católica de Ávila","email":"[email protected]"},
{"id":10,"first_name":"Hube","university":"Universitat Rovira I Virgili Tarragona","email":"[email protected]"}]
/* Request 1: Create a new array called universities that contains all the univeristies in the graduates array.
Once you have the new array created, sort the universities alphabetically and log the result. */
const universities = [];
for(let i = 0; i < graduates.length; i++) {
universities.push(graduates[i].university);
universities.sort();
}
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 protected]
Log the result of your new array. */
const contactInfo = [];
for(let i = 0; i < graduates.length; i++) {
contactInfo.push(graduates[i].first_name);
contactInfo.push(graduates[i].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 (let i = 0; i < graduates.length; i++) {
if (graduates[i].university.includes("Uni")) {
uni.push(graduates[i].university);
}
}
console.log(uni);
// ==== Array Methods ====
// Given this zoo data from around the United States, follow the instructions below
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"}];
// The zoos need a list of all their animal's names 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.
let lowerCase = zooAnimals.map((animal) => {
return animal.animal_name.toLowerCase();
});
console.log(lowerCase);
// The zoos need to know their total animal population across the United States. Add up all the population numbers from all the zoos using the .reduce() method.
let populationTotal = zooAnimals.reduce((total, one) => {
return total += one.population;
}, 0);
console.log(populationTotal);