最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - listToArray Eloquent JavaScript - Stack Overflow

programmeradmin1浏览0评论

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));

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));
Share Improve this question edited May 13, 2015 at 20:24 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked May 13, 2015 at 20:22 Jenny Jenny 234 bronze badges 1
  • 1 For what it's worth in modern JavaScript you'd make the list iterable rather than rolling your own iteration scheme. – Benjamin Gruenbaum Commented May 13, 2015 at 20:38
Add a ment  | 

4 Answers 4

Reset to default 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).

...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 pare list itself:

while (list != null)

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

this will turn the list into an array

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

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

    arrayResult.push(listValue.value);

    return arrayResult;
}
发布评论

评论列表(0)

  1. 暂无评论