Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 249807e

Browse files
committed
Incorporated feedback from Maartje
1 parent 19c76db commit 249807e

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

Week7/REVIEW.md

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# REVIEW JavaScript week 7
22

3+
```
4+
This review covers:
5+
• Git Workflow
6+
• Map,
7+
• Reduce
8+
• Filter
9+
```
10+
311
## Git Workflow
412

5-
To be provided
13+
Please refer to [Tutorial for HackYourFuture Git pull requests and collaboration workflow](https://github.com/HackYourFuture/Git/blob/master/Lecture-3.md)
614

715
## Map, filter, reduce
816

917
The array methods **map()**, **filter()** and **reduce()** are best understood by looking at how they could be implemented if we were to write them ourselves. In the next few sections we will present simplified versions of the native implementations. We have prefixed the method names with `my` to distinguish them from the built-in versions.
1018

19+
Each of the three methods use a `for` loop internally. You will notice that once you start using these methods the need for `for` loops in your own code is greatly reduced (hurray!).
20+
1121
### Array#map\*
1222

1323
The **map** method returns a new array where each element of the subject array is transformed by a user-supplied transformation (= _mapping_) function.
@@ -24,22 +34,52 @@ Array.prototype.myMap = function (mapFn) {
2434

2535
<small>\* Array#map is a short-hand notation for Array.prototype.map.</small>
2636

37+
Because the **map()** method is called on an array (using dot-notation), the value of `this` refers to that array itself (in this review called the _subject_ array).
38+
39+
Internally, the **map()** method initializes a new, empty array to which it will push transformed elements, one by one, as it iterates through the subject array, calling the `mapFn` function for each individual element. When the loop has been completed, the new array is returned. Note that the subject array itself remains unmodified.
40+
41+
`this[i]` refers to an element of the subject array at loop index 'i' (because `this` is a reference to the subject array).
42+
2743
As you can see, the `mapFn` function is called with three arguments:
2844

2945
1. the current array element to be transformed
3046
2. the index of the element (starting with `0`)
3147
3. the subject array itself
3248

33-
As is usual in JavaScript you do not necessarily have to use all the parameters that were passed to the `mapFn` function. In fact, in many cases you will only need the first argument (the current array element).
34-
35-
In the example below we will use the Array#map method to compute the squares of an array of numbers.
49+
In the example below we will use the Array#map method to create a new array that holds the squares of a subject array of numbers. The mapping function is represented by an ES6 fat arrow function:<br>`num => num * num`
3650

3751
```js
3852
const numbers = [3, 5, 2, 7];
3953
const squares = numbers.map(num => num * num);
4054
console.log(squares); // -> [9, 25, 4, 49]
4155
```
4256

57+
For illustrative purposes we can add a `console.log` statement to our mapping function and see what we get passed as second and third argument:
58+
59+
```js
60+
const numbers = [3, 5, 2, 7];
61+
const mapFn = (num, index, arr) => {
62+
console.log(num, index, arr);
63+
return num * num;
64+
}
65+
const squares = numbers.map(mapFn);
66+
console.log('squares', squares)
67+
```
68+
69+
Output:
70+
71+
```js
72+
3 0 [ 3, 5, 2, 7 ]
73+
5 1 [ 3, 5, 2, 7 ]
74+
2 2 [ 3, 5, 2, 7 ]
75+
7 3 [ 3, 5, 2, 7 ]
76+
squares [ 9, 25, 4, 49 ]
77+
```
78+
79+
For each of the first four lines in the output (from the `console.log` inside the `for` loop) the first number is the value of the current element, the second number is the current loop index value and the array value is the original subject array.
80+
81+
As is usual in JavaScript you do not necessarily have to use all the parameters that were passed to the `mapFn` function. In fact, in many cases you will only need the first argument (the current array element) as we saw in the first example.
82+
4383
### Array#filter
4484

4585
The **filter()** method returns a new array with all elements that pass the test implemented by a user-supplied (predicate\*) function.
@@ -58,7 +98,9 @@ Array.prototype.myFilter = function (predicateFn) {
5898

5999
<small>\*A predicate is a function that returns a boolean, whose value depends on its supplied arguments.</small>
60100

61-
Example:
101+
This method works in a similar fashion as the **map()** method, but now elements are only pushed to the new array if the predicate function returns `true`.
102+
103+
In the example below the predicate function test whether the current element is even by checking whether its value divided by two has a remainder of zero. The result of this comparison (`true` or `false`) is the return value of the predicate and determines whether the current element gets added to the new array or not.
62104

63105
```js
64106
const numbers= [6, 3 , 10, 1];
@@ -84,6 +126,20 @@ Array.prototype.myReduce = function (reducerFn, initialValue) {
84126
};
85127
```
86128

129+
The key to understanding the **reduce()** method is in the line:
130+
131+
```js
132+
accumulator = reducerFn(accumulator, this[i], i, this);
133+
```
134+
135+
In the case we don't need the current loop index and the subject array in the reducer function (which is oftent the case), we can simplify this to:
136+
137+
```js
138+
accumulator = reducerFn(accumulator, this[i]);
139+
```
140+
141+
From this line we can define the reducer function as a function that takes an accumulator value and the current array element and returns a new accumulator value.
142+
87143
The whole process is visualised in the figure below (the term _bucket_ was used here to represent the accumulator).
88144

89145
![image](assets/reduce-bucket.png)
@@ -129,7 +185,7 @@ const arr = [
129185
{ gender: 'F', name: 'Lucy' },
130186
{ gender: 'M', name: 'Ferdinand' }
131187
];
132-
const groupedNames = arr.myReduce((acc, elem) => {
188+
const groupedNames = arr.reduce((acc, elem) => {
133189
if (acc[elem.gender]) {
134190
acc[elem.gender].push(elem);
135191
} else {

0 commit comments

Comments
 (0)