-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.js
253 lines (174 loc) Β· 9.11 KB
/
index.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
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
//πππ Topic #1 Closures πππ//
/* ππππ€ Task 1: π€πππ
Study the code below and explain in your own words why nested function can access the variable internal. */
const external = "I'm outside the function";
function myFunction() {
console.log(external);
const internal = "Hello! I'm inside myFunction!";
function nestedFunction() {
console.log(internal);
}
nestedFunction();
}
//myFunction();
//πππ β¬οΈ π Explanation β¬οΈ π πππ:
/* πππ Task 2: Counter πππ */
/* Use summation to do the following:
1. Receive a number as an argument passed from a parameter
2. Use a counter to return the summation of that number.
π EXAMPLE: invoking `summation(4)` should return 10 because 1+2+3+4 is 10.
π‘ NOTE: you may use a for loop for this function if you wish
*/
function summation(/*Your Code Here*/) {
/*Your Code Here*/
}
// π¦π¦π¦ Topic 2: 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.
1. Receive the zooAnimals array as an argument passed from a parameter
2. Use .forEach() to populate a new array called displayNames that will be an array of strings with only the animal name and scientific name of each animal
3. Return the new array
π‘ NOTE: the array returned should be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}"
*/
function animalNames(/*Your Code Here*/){
/*Your Code Here*/
}
/* π¦π¦π¦ Request 2: .map() π¦π¦π¦
The zoo needs a list of all their animal's names converted to lower case.
Use lowerCaseNames to do the following:
1. Receive the zooAnimals array as an argument passed from a parameter
2. Use .map() to create a new array of strings with the animal's names in lowercase
3. Return the new array
π EXAMPLE of returned array: ['jackal, asiatic', .....]
π‘ NOTE: Do some research for other methods that can help help you
*/
function lowerCaseNames(/*Your Code Here*/){
/*Your Code Here*/
}
/* π¦π¦π¦ Request 3: .filter() π¦π¦π¦
The zoo is concerned about animals with a lower population count.
Use lowPopulationAnimals to do the following:
1. Receive the zooAnimals array as an argument passed from a parameter
2. Use .filter() to create a new array of objects which contains only the animals with a population of less than 5
3. Return this new array
*/
function lowPopulationAnimals(/*Your Code Here*/){
/*Your Code Here*/
}
/* π¦π¦π¦ Request 4: .reduce() π¦π¦π¦
The zoo needs to know their total animal population across the United States.
USe USApop to do the following:
1. Receive the zooAnimals array as an argument passed from a parameter
2. Use the .reduce() method to find the total population from the zoosAnimals array
3. Return the total population
π‘ NOTE: Remember the reduce method takes two arguments: a callback (which itself takes two args - the accumulator and the item), and an initial value for the count. Check MDN/W3Schools for syntax!
*/
function USApop(/*Your Code Here*/){
/*Your Code Here*/
}
// π¦π¦π¦ Callbacks π¦π¦π¦
/* π¦π¦π¦ Step 1: Create a higher-order function π¦π¦π¦
Use the higher-order function called consume to do the following:
1. Receive 3 parameters: a, b and cb. The first two parameters (a and b) can take any argument (we can pass any value as an argument) and the last parameter (cb) accepts a callback
2. Return the invocation of cb taking `a` and `b` as its arguments
π‘ NOTE: The tests for 'consume' will pass if it is created correctly and also after you correctly complete the functions 'add' and 'greeting' below in Step 2.
*/
function consume(/*Your Code Here */){
/*Your Code Here */
}
// π¦π¦π¦ Step 2: Create several functions to callback with consume(); π¦π¦π¦
/* Use add to do the following:
1. Receive two numbers as an argument that are passed in from its first and second parameters
2. Return the sum of those numbers
*/
function add(/*Your Code Here */){
/*Your Code Here*/
}
/* Use multiply to do the following:
1. Receive two numbers as an argument that are passed in from its first and second parameters
2. Return the product of those numbers
*/
function multiply(/*Your Code Here */){
/*Your Code Here */
}
/* Use greeting to do the following:
1. Receive two strings (a first name and last name) as an argument that are passed in from its first and second parameters
2. Return "Hello {first-name} {last-name}, nice to meet you!"
π‘ NOTE: The string returned must match the format above or the test will not pass!
*/
function greeting(/*Your Code Here */){
return /*Your Code Here */
}
// π¦π¦π¦ 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!
// π΄π΄π΄ Topic 3: Prototypes π΄π΄π΄ //
//π΄π΄π΄ Task: You are to build a cuboid maker that can return values for a cuboid's volume or surface area. Cuboids are similar to cubes but do not have even sides. Follow the steps in order to accomplish this challenge. π΄π΄π΄
/* π΄π΄π΄ Step 1: Base Constructor π΄π΄π΄
Use CuboidMaker to do the following:
- Receives a single argument -- an object with the follwoing keys:
+ length
+ width
+ height
- Instances of CuboidMaker should initialize `length`, `width` and `height` properties
*/
function CuboidMaker(/*Your Code Here */){
/*Your Code Here */
}
/* π΄π΄π΄ Step 2: Volume Method π΄π΄π΄
Create a method called volume using CuboidMaker's prototype that returns the volume of a given cuboid's length, width, and height
π‘ NOTE: Formula for cuboid volume: length * width * height
*/
/* π΄π΄π΄ Step 3: Surface Area Method π΄π΄π΄
Create another method called surfaceArea using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height.
π‘ NOTE: Formula for cuboid surface area: 2 * (length * width + length * height + width * height)
*/
/* π΄π΄π΄ Step 4: Create a new object that uses CuboidMaker (not auto graded)π΄π΄π΄
Create an object called cuboid that uses the new keyword to use our CuboidMaker constructor
Add properties and values of length: 4, width: 5, and height: 5 to cuboid. */
// π΄π΄π΄ Test your volume and surfaceArea methods by uncommenting the logs below: π΄π΄π΄
// β¬οΈ β¬οΈ β¬οΈ β¬οΈ β¬οΈ β¬οΈ β¬οΈ β¬οΈ
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
// π¦π¦π¦ Topic 4: Classes π¦π¦π¦ //
//Using CuboidMakerTwo, take your prototypes from above and refactor into class syntax. Then, create an object called cuboidTwo that uses the new keyword to use our CuboidMakerTwo class.
class CuboidMakerTwo{
}
//π¦π¦π¦ Test your volume and surfaceArea methods by uncommenting the logs below: π¦π¦π¦
// console.log(cuboidTwo.volume()); // 100
// console.log(cuboidTwo.surfaceArea()); // 130
/* πππππ Please do not modify anything below this line πππππ */
function foo(){
console.log('its working');
return 'bar';
}
foo();
module.exports = {
foo,
summation,
animalNames,
lowerCaseNames,
lowPopulationAnimals,
USApop,
consume,
add,
multiply,
greeting,
CuboidMaker,
CuboidMakerTwo
}