I am working on an exercise from the book Eloquent JavaScript (see 'a list' at bottom of linked page). Basically, I want to make an object that takes an array and turns it into a list with the following structure:
var list = {
value: 1,
rest: {
value: 2,
rest: {
value: 3,
rest: null
}
}
};
When I run the code below in the console, I get TypeError: Cannot read property 'length' of undefined (line 3 in function arrayToList)
. Why is the array on which I am calling .length not defined?
function arrayToList(array){
var list = {};
if(!array.length){
list.value = null;
}
else{
list.value = array.shift();
list.rest = arrayToList();
}
return list;
}
console.log(arrayToList([10, 20]));
Note: Stack Overflow question List data structures in JavaScript is a very similar post on the same problem. I'm not looking for a solution here so much as an explanation as to what is going haywire in the recursive call.
list.rest = arrayToList();
Since you don't pass any parameter to arrayToList
, array
will have the default value undefined
. That is why it is failing with
TypeError: Cannot read property 'length' of undefined
So, during the recursive call, you are supposed to pass an array to arrayToList
, again.
function arrayToList(array) {
var list = {};
if (!array.length) {
list.value = null;
} else {
list.value = array.shift();
list.rest = arrayToList(array); // Pass the `array` after shifting
}
return list;
}
console.log(arrayToList([10, 20]));
# { value: 10, rest: { value: 20, rest: { value: null } } }
On this line:
list.rest = arrayToList();
… you recursively call the function but you don't specify an argument, so array
becomes undefined
.
It looks like you should just pass array
again (which you have modified).
list.rest = arrayToList(array);
live demo
In the recursive arrayToList
call, you're not passing anything as a parameter (!).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With