5 kyu

A Chain adding function

18,423 of 29,289gelus

Description:

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

add(1)(2); // == 3
add(1).(2); # equals 3
add(1)(2) # equals 3

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

add(1)(2)(3); // == 6
add(1)(2)(3)(4); //  == 10
add(1)(2)(3)(4)(5); // == 15
add(1).(2).(3); # 6
add(1).(2).(3).(4); # 10
add(1).(2).(3).(4).(5); # 15
add(1)(2)(3) # 6
add(1)(2)(3)(4); # 10
add(1)(2)(3)(4)(5) # 15

and so on.

A single call should be equal to the number passed in.

add(1); // == 1
add(1); # 1
add(1) # 1

We should be able to store the returned values and reuse them.

var addTwo = add(2);
addTwo; // == 2
addTwo + 5; // == 7
addTwo(3); // == 5
addTwo(3)(5); // == 10
var addTwo = add(2);
addTwo; # 2
addTwo + 5; # 7
addTwo(3); # 5
addTwo(3).(5); # 10
addTwo = add(2)
addTwo # 2
addTwo + 5 # 7
addTwo(3) # 5
addTwo(3)(5) # 10

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

Mathematics
Functional Programming
Puzzles

More By Author:

Check out these other kata created by gelus

Stats:

CreatedJun 12, 2014
PublishedJun 12, 2014
Warriors Trained86314
Total Skips16737
Total Code Submissions123777
Total Times Completed29289
JavaScript Completions18423
Python Completions8462
TypeScript Completions1196
Ruby Completions544
C++ Completions1023
Total Stars2427
% of votes with a positive feedback rating82% of 2317
Total "Very Satisfied" Votes1680
Total "Somewhat Satisfied" Votes432
Total "Not Satisfied" Votes205
Ad
Contributors
  • gelus Avatar
  • jhoffner Avatar
  • natan Avatar
  • Anseltis Avatar
  • svieira Avatar
  • zoobestik Avatar
  • siebenschlaefer Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • trashy_incel Avatar
Ad