davidktw
Arch-Supremacy Member
- Joined
- Apr 15, 2010
- Messages
- 13,547
- Reaction score
- 1,300
I know there are some bright engineers here. Here I have a question for anyone interested to just give it a try, just for fun.
Are you able to design a Javascript 'add' function that take in at least one argument, and up to in theory "infinite" number of arguments (limited by the amount of memory available) and capable of evaluating test codes below.
You can code using any particular standard of Javascript, I would say stick with ECMA2015 since it is the popular one right now supported by most modern browsers. You are free to code any number of helper functions as long as there is an 'add' function that can execute the test codes below, and will produce the EXACT output as accordingly.
There is no need to consider on performance, good or bad intent of the exercise, good or bad practices, or whatsoever reason. Basically all it matters is if you can design such a function that will work as accordingly.
It should pass the following set of testing codes and produce the following outputs below.
Testing Codes:
Expected Output:
For the sake of other interested parties to try, do POST your codes inside SPOILER tags so that they can try without looking at your solution, unless they wanted to.
Feel free to give it a try!
I will upload my solution, after lets say a week or more depending on activeness of this thread.
Happy coding.
UPDATE: 11-Aug-2019
Seems like there wouldn't be much activity, hence my solution is provided below. I will put in the SPOILER so that if anyone notice this thread and want to give it a go, feel free to do so.
Are you able to design a Javascript 'add' function that take in at least one argument, and up to in theory "infinite" number of arguments (limited by the amount of memory available) and capable of evaluating test codes below.
You can code using any particular standard of Javascript, I would say stick with ECMA2015 since it is the popular one right now supported by most modern browsers. You are free to code any number of helper functions as long as there is an 'add' function that can execute the test codes below, and will produce the EXACT output as accordingly.
There is no need to consider on performance, good or bad intent of the exercise, good or bad practices, or whatsoever reason. Basically all it matters is if you can design such a function that will work as accordingly.
It should pass the following set of testing codes and produce the following outputs below.
Testing Codes:
Code:
console.log(add(-1));
console.log(add(0));
console.log(add(1));
console.log(add(1)(2));
console.log(add(1)(2)(3));
console.log(1000 + add(1)(2)(3));
console.log(add(1)(2)(3) - 1000);
console.log(add(1)(2)(3) * 10);
console.log(10 * add(1)(2)(3));
var s = "add(" + [...Array(1000).keys()].join(")(") + ")";
eval("console.log(" + s + ")");
Expected Output:
Code:
-1
0
1
3
6
1006
-994
60
60
499500
For the sake of other interested parties to try, do POST your codes inside SPOILER tags so that they can try without looking at your solution, unless they wanted to.
Feel free to give it a try!
I will upload my solution, after lets say a week or more depending on activeness of this thread.
Happy coding.
UPDATE: 11-Aug-2019
Seems like there wouldn't be much activity, hence my solution is provided below. I will put in the SPOILER so that if anyone notice this thread and want to give it a go, feel free to do so.
Before you go further, have you try it out of your own ? If you have not, my hints are as follows
[1] Usage of Closures
[2] Understanding First Class Functions in functional languages and its approach
[3] Understanding Curry and Function Composition, also part of functional programming techniques
Generally,
F(X)(Y)(Z) is effectively ((F(X))(Y))(Z). It is a function that returns a function, which is why it seems like there is a stacking of parentheses.
With the above hints, you can study on them and see if you can solve the problem. If you insist that you want to see the solution, go ahead
Someone else in this thread has provided a similar variant solution to mine, which is right too, you can also consider that particular way of solving this solution.
[1] Usage of Closures
[2] Understanding First Class Functions in functional languages and its approach
[3] Understanding Curry and Function Composition, also part of functional programming techniques
Generally,
F(X)(Y)(Z) is effectively ((F(X))(Y))(Z). It is a function that returns a function, which is why it seems like there is a stacking of parentheses.
With the above hints, you can study on them and see if you can solve the problem. If you insist that you want to see the solution, go ahead
Code:
const add = x => {
var s = 0;
var q = x => { s += x; return q; }
q.toString = () => s;
return q(x);
}
Someone else in this thread has provided a similar variant solution to mine, which is right too, you can also consider that particular way of solving this solution.
Last edited:
