educative.io

Is the Javascript answer correct

I know running it as it is gives the correct solution. What I am doing is, not using the dequeue and instead using a normal array to record the currentPath and so instead of doing allPaths.push(currentPath.toArray()), I am just doing allPaths.push(currentPath).

This does not work. I get back [[], []]. For some reason, Dequeue plays a part in this which I cannot understand why.

According to what I understand, currentPath’s value depends on the recursion level we are on… so when we are at leaf level, the currentPath is complete and if Sum = 0 we add it to allPaths.
But the same should apply for allPaths, so if we add a currentPath to allPaths at leaf level, then when we move 1 level up or any no of levels up… allPaths should go back to being empty (as currentPath is only being added at leaf levels). What I mean to say is, we are not returning anything so how is allPaths persisting??

sorry about this post, I was making a stupid mistake of not pushing copy of currentPath to allPaths.

Don’t apologize. I was wondering the same exact question and you helped me answer it, so thank you!

Made the same mistake here. Thanks for posting

I see now. When pushing currentPath.slice() works.