Skip to content

Commit ab5fb7b

Browse files
committed
Added and completed "each" in Underbar.
1 parent 8e477f5 commit ab5fb7b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/underbar.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ var _ = {};
5050
// Note: _.each does not have a return value, but rather simply runs the
5151
// iterator function over each item in the input collection.
5252
_.each = function(collection, iterator) {
53+
//test if collection is array
54+
if (Array.isArray(collection)) {
55+
for (var i=0; i<collection.length; i++) {
56+
iterator(collection[i], i, collection);
57+
}
58+
//otherwise treat as object
59+
} else {
60+
for (var j in collection) {
61+
iterator(collection[j], j, collection);
62+
}
63+
}
5364
};
5465

5566
// Returns the index at which value can be found in the array, or -1 if value

0 commit comments

Comments
 (0)