diff --git a/assignments/q1-multiples.js b/assignments/q1-multiples.js index ebe5e18..67c1d39 100644 --- a/assignments/q1-multiples.js +++ b/assignments/q1-multiples.js @@ -1,3 +1,11 @@ +// Find the sum of all positive multiples of 3 or 5 below a specified number (limit). + function multiples (limit) { - // ... -} + var sum = 0; + for (var i = 1; i < limit; i++) { + if (i % 3 == 0 || i % 5 == 0) { + sum = sum + i; + } + } + return sum; +} \ No newline at end of file diff --git a/assignments/q2-map.js b/assignments/q2-map.js index a01bc71..f05db5c 100644 --- a/assignments/q2-map.js +++ b/assignments/q2-map.js @@ -1,3 +1,7 @@ function map (xs, fn) { - // ... -} + var trans = new Array(); + for (var x in xs) { + trans.push(fn(xs[x])); + } + return trans; +} \ No newline at end of file diff --git a/assignments/q3-filter.js b/assignments/q3-filter.js index 757f6f1..a65add1 100644 --- a/assignments/q3-filter.js +++ b/assignments/q3-filter.js @@ -1,3 +1,8 @@ function filter (xs, condition) { - // ... + var filtered = new Array(); + for (var x in xs) { + if (condition(xs[x]) ) + filtered.push(xs[x]); + } + return filtered; } diff --git a/assignments/q4-reduce.js b/assignments/q4-reduce.js index becc358..278b6ed 100644 --- a/assignments/q4-reduce.js +++ b/assignments/q4-reduce.js @@ -1,4 +1,10 @@ function reduce (xs, fn, seed) { - // NOTE: that seed is optional; you should give the appropriate default in the function body - // ... -} + // NOTE: that seed is optional; you should give the appropriate default in the function body + + var init = (typeof seed === "undefined") ? xs[0] : seed; + var i = (typeof seed === "undefined") ? 1 : 0; + + for (i; i < xs.length; i++) + init = fn(init, xs[i]); + return init; +} \ No newline at end of file