Before I get to the meat of my question, here is the code that goes before the area in question.
function arrayToList(array) {
var list = null;
for (var i = array.length - 1; i >= 0; i--)
list = {value: array[i], rest: list};
return list;
}
function listToArray(list) {
var array = [];
for (var node = list; node; node = node.rest)
array.push(node.value);
return array;
}
Now, can someone explain the difference between calling a function and returning a function. When I call my function, I get undefined for my result. However if I return my function, the answer is correct. Can someone explain to me the difference between the two?
With the return:
function nth(list, n) {
if (!list)
return undefined;
else if (n == 0){
return list.value;
}
else
return nth(list.rest, n - 1);
}
Without the return:
function nth(list, n) {
if (!list)
return undefined;
else if (n == 0){
return list.value;
}
else
nth(list.rest, n - 1);
}
Thanks for your help!
returndo? Why would you even think that a function which does not return a value could return a correct value?