Skip to content

Instantly share code, notes, and snippets.

@shime
Created November 19, 2014 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shime/3637070212e9596fd0b4 to your computer and use it in GitHub Desktop.
Save shime/3637070212e9596fd0b4 to your computer and use it in GitHub Desktop.
text from kata at http://www.codewars.com/kata/a-chain-adding-function/. solution is in 3 lines of code.

We want to create a function that will add numbers together when called in succession.

add(1)(2) == 3 // true

We also want to be able to continue to add numbers to our chain.

add(1)(2)(3) == 6 // true
add(1)(2)(3)(4) == 10 // true
add(1)(2)(3)(4)(5) == 15 // true

and so on.

A single call should return the number passed in.

add(1) == 1 // true

and we should be able to store the result and reuse it.

var addTwo = add(2);
addTwo == 2 // true
addTwo(3) == 5 // true
addTwo(3)(5) == 10 // true

We can assume any number being passed in will be valid javascript number.

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