Skip to content

Commit 2b951bd

Browse files
committed
Fixed some styling with q1, and added q2: map
1 parent 666e2f4 commit 2b951bd

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

2013-02-03-assignment.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ Again, please feel free to post questions on [github Issues page][issues] for th
1111
Find the sum of all positive multiples of 3 or 5 below a specified number (`limit`).
1212

1313
```javascript
14-
function multiples(limit) {
14+
function multiples (limit) {
1515
// ...
1616
}
1717
```
1818

1919
Example Tests:
2020

2121
```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
2525
```
2626

2727
### Hint
@@ -34,8 +34,29 @@ Note that `limit` is **non-inclusive**, so if the limit was `15`, the list of nu
3434

3535
## Q2: Implement Map
3636

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+
function map (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+
3754
### Hint
3855

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+
3960
---
4061

4162
## Q3: Implement Filter
@@ -60,5 +81,7 @@ Note that `limit` is **non-inclusive**, so if the limit was `15`, the list of nu
6081

6182

6283
<!-- links -->
84+
[enumerable]: http://ruby-doc.org/core-1.9.3/Enumerable.html
85+
[underscore]: http://underscorejs.org
6386
[issues]: https://github.com/Duke-PL-Course/JavaScript/issues?state=open
6487
[help]: https://help.github.com/articles/fork-a-repo

assignments/q1-multiples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
function multiples(limit) {
1+
function multiples (limit) {
22
// ...
33
}

assignments/q2-map.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function map (xxs, fn) {
2+
// ...
3+
}

0 commit comments

Comments
 (0)