Skip to content

Instantly share code, notes, and snippets.

@subtubes-io
Created April 17, 2014 03:30
Show Gist options
  • Select an option

  • Save subtubes-io/10950989 to your computer and use it in GitHub Desktop.

Select an option

Save subtubes-io/10950989 to your computer and use it in GitHub Desktop.
My version of Array reduce
it("Should reduce", function () {
var result;
var _ = {
reduce: function () {
var reduce = function (arr, func) {
var i = 0;
var len = arr.length;
var running;
for (i; i < len; i++) {
if (i === 0) {
running = func(arr[i], arr[i + 1]);
i++;
} else {
running = func(running, arr[i]);
}
}
return running;
};
if (Array.prototype.reduce) {
_.reduce = function (someArr, someFunc) {
return someArr.reduce(someFunc);
};
} else {
_.reduce = reduce;
}
return _.reduce(arguments[0], arguments[1]);
}
};
result = _.reduce([0, 1, 2, 3], function (one, two) {
return one + two;
});
expect(result).toBe(6);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment