Skip to content
Advertisement

Using recursion to determine the index path of a nested function

Im trying to make a function which finds a value from a list (xs) using another list (index_list) as an index path.

My function should work like this:

JavaScript

So far I have:

JavaScript

This however just returns 0 for everything, but I don’t know what else the base case should be.

Advertisement

Answer

You’re quite close, but you forgot that at each recursion you actually need to index the list so that you get further in at each recursion. This way, by the time you get to the base case, the variable xs will store the correct result and you can just return it.

This is what the code would look like:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement