Skip to content

Commit

Permalink
Product and SumCubes
Browse files Browse the repository at this point in the history
  • Loading branch information
coltoncockman committed Jun 17, 2013
1 parent dc641ad commit 98e5ebb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ lumenize.map
deploy
.js
.map
play
39 changes: 39 additions & 0 deletions src/functions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/functionsTest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98e5ebb

Please sign in to comment.