Skip to content

Instantly share code, notes, and snippets.

@yangchenyun
Created July 29, 2012 14:15
Show Gist options
  • Save yangchenyun/3199164 to your computer and use it in GitHub Desktop.
Save yangchenyun/3199164 to your computer and use it in GitHub Desktop.
sample calculator method
calculator = (method, numbers...) ->
switch method
when 'multiple' then @multiple(numbers)
when 'addition' then @addition(numbers)
when 'minus' then @minus(numbers)
when 'divide' then @divide(numbers)
else
throw new Error "method doesn't exist"
multiple = (numbers) ->
if numbers.length <= 1
throw new Error 'multiple takes more than one operands'
else
numbers.reduce (prev, curr) ->
prev * curr
addition = (numbers) ->
if numbers.length <= 1
throw new Error 'addition takes more than one operands'
else
numbers.reduce (prev, curr) ->
prev + curr
minus = (numbers) ->
if numbers.length isnt 2
throw new Error 'minus takes only two operands'
else
numbers[0] - numbers[1]
divide = (numbers) ->
if numbers.length isnt 2
throw new Error 'divide takes only two operands'
else
numbers[0] / numbers[1]
module.exports =
calculator: calculator
multiple: multiple
addition: addition
minus: minus
divide: divide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment