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: Week7/REVIEW.md
+62-6Lines changed: 62 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,23 @@
1
1
# REVIEW JavaScript week 7
2
2
3
+
```
4
+
This review covers:
5
+
• Git Workflow
6
+
• Map,
7
+
• Reduce
8
+
• Filter
9
+
```
10
+
3
11
## Git Workflow
4
12
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)
6
14
7
15
## Map, filter, reduce
8
16
9
17
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.
10
18
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
+
11
21
### Array#map\*
12
22
13
23
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) {
24
34
25
35
<small>\* Array#map is a short-hand notation for Array.prototype.map.</small>
26
36
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
+
27
43
As you can see, the `mapFn` function is called with three arguments:
28
44
29
45
1. the current array element to be transformed
30
46
2. the index of the element (starting with `0`)
31
47
3. the subject array itself
32
48
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`
36
50
37
51
```js
38
52
constnumbers= [3, 5, 2, 7];
39
53
constsquares=numbers.map(num=> num * num);
40
54
console.log(squares); // -> [9, 25, 4, 49]
41
55
```
42
56
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
+
constnumbers= [3, 5, 2, 7];
61
+
constmapFn= (num, index, arr) => {
62
+
console.log(num, index, arr);
63
+
return num * num;
64
+
}
65
+
constsquares=numbers.map(mapFn);
66
+
console.log('squares', squares)
67
+
```
68
+
69
+
Output:
70
+
71
+
```js
72
+
30 [ 3, 5, 2, 7 ]
73
+
51 [ 3, 5, 2, 7 ]
74
+
22 [ 3, 5, 2, 7 ]
75
+
73 [ 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
+
43
83
### Array#filter
44
84
45
85
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) {
58
98
59
99
<small>\*A predicate is a function that returns a boolean, whose value depends on its supplied arguments.</small>
60
100
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.
62
104
63
105
```js
64
106
constnumbers= [6, 3 , 10, 1];
@@ -84,6 +126,20 @@ Array.prototype.myReduce = function (reducerFn, initialValue) {
84
126
};
85
127
```
86
128
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
+
87
143
The whole process is visualised in the figure below (the term _bucket_ was used here to represent the accumulator).
0 commit comments