Thanks Barbar's comments in StackOverflow, I understand the algorithm now.
The algorithm can be rewritten as follows:
f=nCk=(n,k)=>{//Declare both f and nCk as the same functionleto=n+1/k//o will be the key of function object ff[o]=oinf//Define f[o] based on a nested ternary expression?f[o]//Avoid recalculation if f has key o already :k==0//nC0 is always 1?1:n<k//nCk is improper and assigned 0 if n<k?0:f(--n,k)//Do recursion nCk = (n-1)Ck + (n-1)C(k-1)+f(n,k-1)returnf[o]//Done!}
Thanks Barbar's comments in StackOverflow, I understand the algorithm now.
The algorithm can be rewritten as follows:
Here goes an example of 5C2
P.S. I got a few takeaways when investigating into this algorithm
Double declaration of function on the same line as a trick for recursion
Immediate use of a key with its value just assigned
Infinity can be used as a key of an object(!)
Syntax o in f checks if object f has the key o