Skip to content

Commit 8ca9d8d

Browse files
authored
Merge pull request #1 from yahiaboudah/added-reduce-polyfill-to-array
Create reduce.js
2 parents 177b4c3 + 8053619 commit 8ca9d8d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Array/reduce.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Array.prototype.reduce = function(callback) {
2+
'use strict';
3+
if (this == null) {
4+
throw new TypeError('Array.prototype.reduce called on null or undefined');
5+
}
6+
if (typeof callback !== 'function') {
7+
throw new TypeError(callback + ' is not a function');
8+
}
9+
var t = Object(this), len = t.length >>> 0, k = 0, value;
10+
if (arguments.length == 2) {
11+
value = arguments[1];
12+
} else {
13+
while (k < len && !(k in t)) {
14+
k++;
15+
}
16+
if (k >= len) {
17+
throw new TypeError('Reduce of empty array with no initial value');
18+
}
19+
value = t[k++];
20+
}
21+
for (; k < len; k++) {
22+
if (k in t) {
23+
value = callback(value, t[k], k, t);
24+
}
25+
}
26+
return value;
27+
};

0 commit comments

Comments
 (0)