I was following the Eloquent Javascript book and making the linked list exercise. I am stuck with recursion. I use the following code:
function arrayToList(array){
var list = {};
length = array.length;
while(array.length > 0){
element = array.pop();
if (length == array.length + 1 )
list = { value: element, rest: null};
else
list = { value: element, rest: list};
}
return list;
}
function nth(list , number){
array = [];
if (list.rest != null && number !=0 ) {
number--;
array.push(list.value);
nth(list.rest, number);
}
answer = list.value
array.push(list.value);
return array[0];
}
example_list = arrayToList([1,2,3,4]);
a = nth(example_list, 3);
console.log(a);
My question now is how the recursion exactly works for the "nth()" function.
I wrote this myself but I can only to get it working when using the array[0] way and I was expecting to be able to just use list.value as the return value. When debugging I see that it goes to the correct value(4), but then it comes back to the first value (1).
So my questions is can this be done, not using an array? It feels like an array is not the correct way and I only want to get the correct value? If any other mistakes in the code gladly get advice about the style as well.