You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 11_Working_with_Arrays/README.md
+27-1Lines changed: 27 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -175,6 +175,32 @@ The names does not matter but the order has to be `element, index, array`
175
175
`for of (index, element)`
176
176
`forEach (element, index, array)`
177
177
178
-
## forEach or for of Loop?
178
+
### forEach or for of Loop?
179
179
180
180
- The forEach method does not have the `continue` and `break` keywords, so use the for of loop if you absolutely want to break or continue a looping operation.
181
+
182
+
# Data Transformations (Maps, Filter & Reduce)
183
+
184
+
The Map, Filter, and Reduce array methods are the three biggest array methods in JavaScript for data transformations. They allow us manipulate and change arrays and store them into newer arrays.
185
+
186
+
In recent years, these tools have become really popular because of their importance.
187
+
188
+
## Map Method (Array.prototype.map())
189
+
190
+
The `map()` method creates a new array populated with the results of calling a provided function on every element in the calling array.
191
+
192
+
This method is similar to the `forEach` method but the difference is that the `Map` methods creates a brand new array based on the original array.
The `filter()` method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function (a certain condition).
The `reduce()` method executes a 'reducer' callback function on each element of an array and returns the sum of all the elements in the array to a single value.
Julia and Kate are doing a study on dogs. So each of them asked 5 dog owners about their dog's age, and stored the data into an array (one array for each). For now, they are just interested in knowing wether a dog is an adult or a puppy. A dog is an adult if it is at least 3 years old, and it's a puppy if it's less than 3 years old.
5
+
6
+
Create a function `checkDogs`, which accepts 2 arrays of dog's ages (`dogJulia` and `dogKate`), and does the following things:
7
+
8
+
1. Julia found out that the owners of the FIRST and the LAST two dogs actually have cats, not dogs! So create a shallow copy of Julia's array, and remove the cat ages from that copied array (because it's bad practice to mutate function parameters)
9
+
10
+
2. Create an array with both Julia's (corrected) and kate's data
11
+
3. For each remaining dog, log to the console wether it's an adult ('Dog number 1 is an adult, and is 5 years old') or a puppy ('Dog number 2 is still a puppy 🐶')
12
+
4. Run the function for both test datasets.
13
+
14
+
HINT: Use tools from all lectures in this section so far. 🙂
0 commit comments