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: 2013-02-03-assignment.md
+27-4Lines changed: 27 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,17 +11,17 @@ Again, please feel free to post questions on [github Issues page][issues] for th
11
11
Find the sum of all positive multiples of 3 or 5 below a specified number (`limit`).
12
12
13
13
```javascript
14
-
functionmultiples(limit) {
14
+
functionmultiples(limit) {
15
15
// ...
16
16
}
17
17
```
18
18
19
19
Example Tests:
20
20
21
21
```javascript
22
-
multiples(1)// 0
23
-
multiples(10)// 23
24
-
multiples(16)// 60
22
+
multiples(1);// 0
23
+
multiples(10);// 23
24
+
multiples(16);// 60
25
25
```
26
26
27
27
### Hint
@@ -34,8 +34,29 @@ Note that `limit` is **non-inclusive**, so if the limit was `15`, the list of nu
34
34
35
35
## Q2: Implement Map
36
36
37
+
The `map` function is one that we saw in Ruby's [`Enumerable`][enumerable] module, and we've seen it in [underscore.js][underscore] as well. We will continue to see it again and again, especially when we explore some of the more functional languages. Because it is such an essential function, we want you to understand how it works by implementing it. In addition, you'll see that the next two questions are in the same vein and ask you to implement two more common functions that are seen in functional languages.
38
+
39
+
`map` will take an array `xxs` of length greater than or equal to 0, and apply a function `fn`, to all elements of the array. `fn` will take a single parameter, and it is the job of `map` to pass each element of the array to `fn`. What `map` will return is an array of the same length as the input; however, the elements of the array will be transformed by `fn`. Please note that you should not change the original values of the input array `xxs`; it should be treated as **immutable**. So, you should create a new array to be returned.
40
+
41
+
```javascript
42
+
functionmap (xxs, fn) {
43
+
// ...
44
+
}
45
+
```
46
+
47
+
Example Tests:
48
+
49
+
```javascript
50
+
map([1, 2, 3, 4, 5], function (x) { return x *2; }); // [2, 4, 6, 8, 10]
51
+
map(['foo', 'bar', 'baz'], function (x) { return x +' is awesome'; }); // ['foo is awesome', 'bar is awesome', 'baz is awesome']
52
+
```
53
+
37
54
### Hint
38
55
56
+
`map`'s job is basically to iterate over all elements of an array, applying a function to each, and returning the collective result as a new array. Don't try to over think this one.
57
+
58
+
Some implementations of `map` in functional languages prefer a recursive implementation involving pattern matching; however, in JavaScript, it will most likely be easier to use an iterative implementation. The choice is ultimately up to you, however.
59
+
39
60
---
40
61
41
62
## Q3: Implement Filter
@@ -60,5 +81,7 @@ Note that `limit` is **non-inclusive**, so if the limit was `15`, the list of nu
0 commit comments