Skip to content

Commit 11ba268

Browse files
committed
problem 4 solved
1 parent 6afccf1 commit 11ba268

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

assignments/q4-reduce.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
function reduce (xs, fn, seed) {
2-
// NOTE: that seed is optional; you should give the appropriate default in the function body
3-
// ...
2+
if (xs.length === 0 && typeof seed === 'undefined')
3+
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+
416
}

0 commit comments

Comments
 (0)