Goodword
Goodword

Reputation: 1645

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.

Upvotes: 10

Views: 2302

Answers (4)

jewelnguyen8
jewelnguyen8

Reputation: 249

function arrayToList(array) {
    var list = {};
    if (!(array instanceof Array) || array.length == 0) {
       list.value = null;
    } else {
        list.value = array.shift();
        list.rest = arrayToList();
    }
    return list;
}

Upvotes: 0

soulprovidr
soulprovidr

Reputation: 754

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

Upvotes: 2

Quentin
Quentin

Reputation: 944568

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

Upvotes: 2

thefourtheye
thefourtheye

Reputation: 239693

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 } } }

Upvotes: 12

Related Questions