Skip to content

Instantly share code, notes, and snippets.

@terryp
Created September 22, 2013 16:54
Show Gist options
  • Save terryp/6661820 to your computer and use it in GitHub Desktop.
Save terryp/6661820 to your computer and use it in GitHub Desktop.
Mildly pedantic way to take command line arguments, coerce into numbers and then add them up.
add = (nums) ->
result = 0
for n in nums
result += n
return result
nums = []
for arg in process.argv[2..]
arg = Number(arg)
nums.push(arg)
console.log(add(nums))
@memoia
Copy link

memoia commented Sep 22, 2013

Even more pedantic:

sum = (list) ->
  [mine, rest] = [parseInt(list[0]), list.slice(1)]
  return (if not rest.length then mine else mine + sum(rest))

console.log(sum(process.argv.slice(2)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment