Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. /*
  2.  
  3. Description:
  4.  
  5. We want to create a function that will add numbers together when called in succession.
  6.  
  7. add(1)(2);
  8. // returns 3
  9. We also want to be able to continue to add numbers to our chain.
  10.  
  11. add(1)(2)(3); // 6
  12. add(1)(2)(3)(4); // 10
  13. add(1)(2)(3)(4)(5); // 15
  14. and so on.
  15.  
  16. A single call should return the number passed in.
  17.  
  18. add(1) // 1
  19. We should be able to store the returned values and reuse them.
  20.  
  21. var addTwo = add(2);
  22. addTwo // 2
  23. addTwo + 5 // 7
  24. addTwo(3) // 5
  25. addTwo(3)(5) // 10
  26. We can assume any number being passed in will be valid javascript number.
  27.  
  28. */
  29.  
  30. function add(n){
  31. var func = function(x) {
  32. return add(n + x);
  33. };
  34. func.valueOf = function() {
  35. return n;
  36. };
  37. return func;
  38. }
  39. console.log(add(1)(2)(5));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement