HWZ Forums

Login Register FAQ Mark Forums Read

Some Fun with Javascript

Like Tree3Likes
Reply
 
LinkBack Thread Tools
Old 07-08-2019, 04:16 PM   #1
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 9,926
Smile Some Fun with Javascript

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:
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:
-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.
Spoiler!

Last edited by davidktw; 11-08-2019 at 11:14 PM..
davidktw is offline   Reply With Quote
Old 08-08-2019, 08:35 AM   #2
Junior Member
 
Join Date: Oct 2007
Posts: 20
Why is it add(1)(2)(3) and not add(1, 2, 3)
micxiao is offline   Reply With Quote
Old 08-08-2019, 09:35 AM   #3
Greater Supremacy Member
 
TrueBeliever_jh's Avatar
 
Join Date: Jun 2006
Posts: 90,993
Spoiler!
__________________
Every Day the Future Looks A Little Bit Darker
TrueBeliever_jh is online now   Reply With Quote
Old 08-08-2019, 10:32 AM   #4
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 9,926
Spoiler!
So have you wonder or know why it works? Would you be able to solve without googling? What are the key techniques to make the solution works
TrueBeliever_jh likes this.

Last edited by davidktw; 08-08-2019 at 10:34 AM..
davidktw is offline   Reply With Quote
Old 08-08-2019, 10:35 AM   #5
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 9,926
Why is it add(1)(2)(3) and not add(1, 2, 3)
Because that is what the question is asking for? Otherwise where is the fun ?

Last edited by davidktw; 08-08-2019 at 10:44 AM..
davidktw is offline   Reply With Quote
Old 08-08-2019, 10:46 AM   #6
Greater Supremacy Member
 
TrueBeliever_jh's Avatar
 
Join Date: Jun 2006
Posts: 90,993
So have you wonder or know why it works? Would you be able to solve without googling? What are the key techniques to make the solution works
Spoiler!
__________________
Every Day the Future Looks A Little Bit Darker

Last edited by TrueBeliever_jh; 08-08-2019 at 10:49 AM..
TrueBeliever_jh is online now   Reply With Quote
Old 08-08-2019, 10:53 AM   #7
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 9,926
Spoiler!
Spoiler!
TrueBeliever_jh likes this.

Last edited by davidktw; 08-08-2019 at 11:10 AM..
davidktw is offline   Reply With Quote
Old 08-08-2019, 09:25 PM   #8
Master Member
 
Join Date: Apr 2003
Posts: 4,485
I have inkling that it looks like LISP.
peterchan75 is offline   Reply With Quote
Old 08-08-2019, 10:07 PM   #9
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 9,926
I have inkling that it looks like LISP.
Functional yes, give it a try?
davidktw is offline   Reply With Quote
Old 08-08-2019, 10:09 PM   #10
Arch-Supremacy Member
 
sanzhu's Avatar
 
Join Date: Nov 2005
Posts: 10,376
This is not cbp okay
sanzhu is offline   Reply With Quote
Old 11-08-2019, 11:13 PM   #11
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 9,926
Solution is provided in the first post, feel free to examine it and understand it. Have fun coding.
TrueBeliever_jh likes this.
davidktw is offline   Reply With Quote
Old 12-08-2019, 04:02 PM   #12
Master Member
 
cwchong's Avatar
 
Join Date: Jan 2005
Posts: 4,073
Why is it add(1)(2)(3) and not add(1, 2, 3)
The former is achieved easily via args
The latter is kinda like currying but recursively
cwchong is online now   Reply With Quote
Old 12-08-2019, 04:03 PM   #13
Senior Member
 
ronronleelee's Avatar
 
Join Date: Aug 2019
Posts: 572
Hahaha wow I donít even know
How to use these codes
ronronleelee is offline   Reply With Quote
Old 12-08-2019, 04:08 PM   #14
Supremacy Member
 
davidktw's Avatar
 
Join Date: Apr 2010
Posts: 9,926
The former is achieved easily via args
The latter is kinda like currying but recursively
Did you mix up between former and latter? No recursion required, suppose we fix to the correct terminology of recursion
davidktw is offline   Reply With Quote
Old 12-08-2019, 04:12 PM   #15
Master Member
 
cwchong's Avatar
 
Join Date: Jan 2005
Posts: 4,073
Some self notes in case i forgot:
The shape of the function should prob be like returning either a value, or a function
Not quite sure how that would be achieved yet, without an end condition like recursion, just keeping inmind

Maybe shaped like
add = (n) => {
return (m) => {
return m+n;
};
}

But of cos this only works for 2 args so needs something else magical

Last edited by cwchong; 12-08-2019 at 04:22 PM..
cwchong is online now   Reply With Quote
Reply
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ. Forum members and moderators are responsible for their own posts.

Please refer to our Terms of Service for more information.


Thread Tools

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On