2

The book says:

To run over a list (in listToArray and nth), a for loop specification like this can be used:

for (var node = list; node; node = node.rest) {}

Every iteration of the loop, node points to the current sublist, and the body can read its value property to get the current element. At the end of an iteration, node moves to the next sublist. When that is null, we have reached the end of the list and the loop is finished.

Question 1: Could you explain how the condition of the for loop works? I understand that it is checking whether the node(the current list) is null.. but how does the "node" argument by itself work?

Question 2: why doesnt the following code work?

function listToArray(list){
    var result = [];
    while(list.value != null){
        result.push(list.value);
        list = list.rest;
    }
    return result;
};

console.log(listToArray(list));
1
  • 1
    For what it's worth in modern JavaScript you'd make the list iterable rather than rolling your own iteration scheme. Commented May 13, 2015 at 20:38

4 Answers 4

5

To understand how this works, you need to know two things:

  • How java script For loop works.
  • What are truthy values.

The syntax for the for loop statement:

for ([initialization]; [condition]; [final-expression]) statement

[condition] An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. If the expression evaluates to false, execution skips to the first expression following the for construct.

In Java script, a truthy value evaluates to true when evaluated in a Boolean context.

All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN).

So till node is null at one point, we may say that node is truthy and evaluates to true.

When the statement, node = node.rest assigns a null value to node, there the for loop exits.

The below code does not work because, list.value may be undefined, or as a fact any other falsy value other than null.

function listToArray(list){
    var result = [];
    while(list.value != null){
        result.push(list.value);
        list = list.rest;
    }
    return result;
};

console.log(listToArray(list));

instead try, while(list.value).

Sign up to request clarification or add additional context in comments.

Comments

2

...but how does the "node" argument by itself work?

node is a variable. A variable is resolved to its value. E.g. if I have

var foo = 3;
alert(foo + 1);

it will resolve foo to the value 3 and then add 1 to it. Similarly in this case, node is resolved to whatever value it has, that value is converted to a Boolean value (i.e. Boolean(node)) and then tested whether it is true or false.

If the value of node is null, then Boolean(node) will return false.

why doesnt the following code work?

Can't really tell without knowing what list is and what exactly "does not work".

But presumably, list.rest is null at some point, in which case you would try to access null.value which throws an error. The equivalent version to the for loop would be to compare list itself:

while (list != null)

Comments

0

A simpler way is to use: var result = Array.prototype.slice.call(list);

this will turn the list into an array

Comments

0
function listToArray(listValue){
     var arrayResult = [];

     while(listValue.list){
        arrayResult.push(listValue.value);
        listValue = listValue.list;
      }

    arrayResult.push(listValue.value);

    return arrayResult;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.