Skip to content

Commit c6869ca

Browse files
committed
Added and completed "reduct" for Underbar.
1 parent 9fb4b74 commit c6869ca

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/underbar.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ var _ = {};
163163
});
164164
};
165165

166-
// Reduces an array or object to a single value by repetitively calling
166+
// Reduces an array of object to a single value by repetitively calling
167167
// iterator(previousValue, item) for each item. previousValue should be
168168
// the return value of the previous iterator call.
169169
//
@@ -177,6 +177,11 @@ var _ = {};
177177
// return total + number;
178178
// }, 0); // should be 6
179179
_.reduce = function(collection, iterator, accumulator) {
180+
var tot = accumulator || 0;
181+
_.each(collection, function(val) {
182+
tot = iterator(tot, val);
183+
});
184+
return tot;
180185
};
181186

182187
// Determine if the array or object contains a given value (using `===`).

0 commit comments

Comments
 (0)