From 49e0bc31aa379b8d20a7dcffeb1a4d1fbda4a472 Mon Sep 17 00:00:00 2001 From: Austin Benesh Date: Thu, 2 May 2013 20:13:39 -0400 Subject: [PATCH] submitting javascript --- assignments/q1-multiples.js | 10 ++++++++-- assignments/q2-map.js | 12 +++++++++--- assignments/q3-filter.js | 15 +++++++++++++-- assignments/q4-reduce.js | 10 ++++++++-- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/assignments/q1-multiples.js b/assignments/q1-multiples.js index ebe5e18..b7c19f1 100644 --- a/assignments/q1-multiples.js +++ b/assignments/q1-multiples.js @@ -1,3 +1,9 @@ function multiples (limit) { - // ... -} + k = limit-1; + k5 = k - k%5; + k3 = k - k%3; + k15 = k - k%15; + return (k5 * (k5 + 5) / 10) + + (k3 * (k3 + 3) / 6) - + (k15 * (k15 + 15) / 30); +} \ No newline at end of file diff --git a/assignments/q2-map.js b/assignments/q2-map.js index a01bc71..b0d3f98 100644 --- a/assignments/q2-map.js +++ b/assignments/q2-map.js @@ -1,3 +1,9 @@ -function map (xs, fn) { - // ... -} +function map(xs, fn) { + var ret = []; + var ctr = 0; + while(ctr < xs.length) { + ret[ctr] = fn(xs[ctr]); + ctr++; + } + return ret; +} \ No newline at end of file diff --git a/assignments/q3-filter.js b/assignments/q3-filter.js index 757f6f1..d64094e 100644 --- a/assignments/q3-filter.js +++ b/assignments/q3-filter.js @@ -1,3 +1,14 @@ function filter (xs, condition) { - // ... -} + var ret = []; + var ctr = 0; + var ctr2 = 0; + while(ctr < xs.length) { + if (condition(xs[ctr])) + { + ret[ctr2] = xs[ctr]; + ctr2++; + } + ctr++; + } + return ret; +} \ No newline at end of file diff --git a/assignments/q4-reduce.js b/assignments/q4-reduce.js index becc358..dc00b8b 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 - // ... + if (seed == null) + seed = xs[0]; + if (xs.length == 0) + return seed; + } + +console.log(reduce([1, 2, 3, 4, 5, 6], function (memo, x) { return memo + x; })); +console.log(reduce([1, 2, 3, 4, 5, 6], function (memo, x) { return memo + x; }, 21));