From 98e5ebbecf06a4a268b9233db38b2b7252cf3f97 Mon Sep 17 00:00:00 2001 From: coltoncockman Date: Sun, 16 Jun 2013 20:37:25 -0400 Subject: [PATCH] Product and SumCubes --- .gitignore | 1 + src/functions.coffee | 39 +++++++++++++++++++++++++++++++++++++++ test/functionsTest.coffee | 16 ++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/.gitignore b/.gitignore index 6cab74d..097ee91 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ lumenize.map deploy .js .map +play diff --git a/src/functions.coffee b/src/functions.coffee index 1b3694d..3070bca 100644 --- a/src/functions.coffee +++ b/src/functions.coffee @@ -51,6 +51,25 @@ functions.sum = (values, oldResult, newValues) -> temp += v return temp +### +@method product +@static +@param {Number[]} [values] Must either provide values or oldResult and newValues +@param {Number} [oldResult] for incremental calculation +@param {Number[]} [newValues] for incremental calculation +@return {Number} The product of the values +### +functions.product = (values, oldResult, newValues) -> + if oldResult? + temp = oldResult + tempValues = newValues + else + temp = 1 + tempValues = values + for v in tempValues + temp = temp * v + return temp + ### @method sumSquares @static @@ -70,6 +89,26 @@ functions.sumSquares = (values, oldResult, newValues) -> temp += v * v return temp +### +@method sumCubes +@static +@param {Number[]} [values] Must either provide values or oldResult and newValues +@param {Number} [oldResult] for incremental calculation +@param {Number[]} [newValues] for incremental calculation +@return {Number} The sum of the cubes of the values +### +functions.sumCubes = (values, oldResult, newValues) -> + if oldResult? + temp = oldResult + tempValues = newValues + else + temp = 0 + tempValues = values + for v in tempValues + temp += v * v * v + return temp + + ### @method lastValue @static diff --git a/test/functionsTest.coffee b/test/functionsTest.coffee index 857b8d9..9f10083 100644 --- a/test/functionsTest.coffee +++ b/test/functionsTest.coffee @@ -175,6 +175,22 @@ exports.functionsTest = test.done() + testSumCubes: (test) -> + + test.equal(functions.sumCubes([3]), 27) + test.equal(functions.sumCubes([1, 2]), 9) + test.equal(functions.sumCubes([0, 5]), 125) + + test.done() + + testProduct: (test) -> + + test.equal(functions.product([3, 7]), 21) + test.equal(functions.product([1, 2]), 2) #any number multiplied by 1 is itself + test.equal(functions.product([0, 5]), 0) #any number multiplied by 0 is 0 + + test.done() + testLastValue: (test) -> test.equal(functions.lastValue([0]), 0) test.equal(functions.lastValue([]), null) #last value of an empty array is null