We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 6afccf1 commit 11ba268Copy full SHA for 11ba268
1 file changed
assignments/q4-reduce.js
@@ -1,4 +1,16 @@
1
function reduce (xs, fn, seed) {
2
- // NOTE: that seed is optional; you should give the appropriate default in the function body
3
- // ...
+ if (xs.length === 0 && typeof seed === 'undefined')
+ return undefined;
4
+ if (typeof seed === 'undefined'){
5
+ seed = xs[0];
6
+ var i = 1;
7
+ } else {
8
+ var i = 0;
9
+ }
10
+ var answer = seed;
11
+ for (i; i < xs.length; i++){
12
+ answer = fn(answer, xs[i]);
13
14
+ return answer;
15
+
16
}
0 commit comments