Created
July 29, 2012 14:15
-
-
Save yangchenyun/3199164 to your computer and use it in GitHub Desktop.
sample calculator method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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