You need to return the value from you recursion…
function nth(list, index){
console.log(index);
if(index < 1){
console.log("Success", list.value);
return list.value;
}
else {
console.log("Fail");
list = list.rest;
index--;
return nth(list,index);
}
}
Think like this –
Initial call I fails, so you recurse R1 and fail, then recurse R2 and succeed.
You correctly return the value from R2 to R1, but you must then return from R1 to I and back out the function.
2
solved Why is this recursive function not returning the value