Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this array undefined when I call .length?

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.

like image 598
Goodword Avatar asked Oct 24 '14 14:10

Goodword


3 Answers

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 } } }
like image 180
thefourtheye Avatar answered Sep 20 '22 07:09

thefourtheye


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

like image 22
Quentin Avatar answered Sep 24 '22 07:09

Quentin


In the recursive arrayToList call, you're not passing anything as a parameter (!).

like image 42
soulprovidr Avatar answered Sep 24 '22 07:09

soulprovidr