< Homepage

DM Puzzle - Function manipulation

Objective

Manipulate pure javascript function to handle arguments.

I would like to get a function named "add" which is able to store and add its arguments (and obviously print it). Simple right ?

Steps :

  1. Create your add function to print its arguments.
  2. I have a really short memory so I need to be able to log in the console the sum of all of the arguments.
  3. Although I take my pills everyday, I sometimes make some mistakes and I have to reset my counter.
  4. We would be happy if our great function could take some arguments like that : add(1,2,3)..
  5. But we'd be very happy if you also could take some arguments like that too : add(1)(2,4)(3)
Bonus:
  1. Print the counter like that : add(4,7)(); // 11
  2. ..And like that : add.reset()(1)(2)(3)()(2,4,5)(2)(); // 6 and 19
  3. Finally, create the entire logic without global functions/variables (all you need is add !)

Now you should be able to have these results :

            add(4);
            add.toString(); // 4

            add.reset();
            add.toString(); // 0

            add(4,7)(); // 11

            add(1)(2)(3)(); // 17

            add.reset()(2)(); // 2

            add.reset()(1)(2)(3)()(2,4,5)(2)(); // 19
        

Prerequisites

Instructions

  1. Keep it fun and simple!
  2. Do it quick and dirty! Do not reach for perfection (you know what I mean)!
  3. Do not spend more than the expected time. If you're having a hard time ask for help or have a look at the solution.
  4. Have fun and if you want to, read more about the puzzle subject once you're done.
    Curiosity is the path to knowledge.

To be resolved before

12, Febuary 2015

Expected time to resolution

Less than 30 minutes

Expected number of lines

Less than 25

Lost?

  • You can use your function "add" as an object to attach someother functions like reset or toString.
  • The recursive way is a good way :)

Definitely lost?

Show solution