generated from jfarmer/template-javascript
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmap.js
40 lines (35 loc) · 1001 Bytes
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Constructs and returns a new array with`fn` to each element of `collection`.
*
* In general,
*
* ```
* map([val1, val2, ..., valN], fn); // => [fn(val1), fn(val2), ..., fn(valN)]
* ```
*
* @example
* function double(x) {
* return x * 2;
* }
*
* map([1, 2, 3, 4], double); // => [2, 4, 6, 8];
*
* @param {any[]} collection - An array containing anything
* @param {function} fn - A function that returns `true` or `false`
* @returns {any[]} A new array constructed by applying `fn` to each
* element of `collection`
*/
function map(collection, fn) {
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
let results = [];
for (let item of collection) {
// This is your job. :)
}
return results;
}
if (require.main === module) {
console.log('Running sanity checks for map:');
// Add your own sanity checks here.
// How else will you be sure your code does what you think it does?
}
module.exports = map;