-
Notifications
You must be signed in to change notification settings - Fork 5
/
source.js
60 lines (50 loc) · 1.7 KB
/
source.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Call iterator(value, key, collection) for each element of collection.
// Accepts both arrays and objects.
//
// Note: _.each does not have a return value, but rather simply runs the
// iterator function over each item in the input collection.
var testArray = [0, 1, 2, 3, 4];
var testObject = {
spoons: 2,
forks: 4,
knives: 8
};
var each = function(collection, iterator) {
if (collection instanceof Array) {
for(var i = 0; i < collection.length; i++)
iterator(collection[i], i, collection);
} else {
for (var key in collection)
iterator(collection[key], key, collection);
}
};
// example use and test
each(testArray, function(item, index, collection) {
collection[index] = item + 2;
});
each(testObject, function(item, index, collection) {
collection[index] = item + 2;
})
console.log("The line below should be [2, 3, 4, 5, 6]")
console.log(testArray);
console.log("The line below should be {spoons: 4, forks: 6, knives: 10}");
console.log(testObject);
// Return all elements of an array that pass a truth test.
var filter = function(collection, test){
// your code here
};
//example use and test
var filteredArray = filter(testArray, function(item, index, collection) {
return item < 5;
});
console.log("The line below should be [2, 3, 4]")
console.log(filteredArray);
// Return the results of applying an iterator to each element.
var map = function(collection, iterator) {
// code goes here
};
var mappedArray = map(testArray, function(item, index, collection) {
return item + 10;
});
console.log("The line below should be [12, 13, 14, 15, 15]");
console.log(mappedArray);