3

I'm reading Eloquent Javascript by Marijn Haverbeke and I'm working on chapter 4's obj to array, and array to obj exercise. The solution to the array to obj is as follow:

var list = {value: 1, rest: { value: 2, rest: { value: 3, rest: null}}};

function listToArray(list){
 var array = [];
 for(var node = list; node; node = node.rest){
  array.push(node.value);
 }
 return array;
}

console.log(listToArray(list));

I'm confused on what is happening on the for loop. This is what I do understand:

  1. variable node is equal to the list object
  2. the length is equal to the node which is the same as the list object
  3. and node is equal to the value rest

Can someone break down what is happening in the this for loop in simple terms?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • It's not a dupe! Argh. – kind user Feb 27 '17 at 21:27
  • https://developer.mozilla.org/ar/docs/Web/JavaScript/Reference/Statements/for – Abdennour TOUMI Feb 27 '17 at 21:27
  • @Kinduser, the underlying question is around how [`for`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) loops work. – zzzzBov Feb 27 '17 at 21:28
  • This is basically a self-contained linked-list, where the `rest` property refers to the next element. The for loop runs until node is null, just like you would iterate a linked-list. So it starts on the outside and works its way inward, adding the value to the array using `push()` as it goes along. – mhodges Feb 27 '17 at 21:28
  • @zzzzBov *underlying* is a keyword. – kind user Feb 27 '17 at 21:29
  • @Kinduser [the first answer to the linked question sufficiently answers this question IMO](http://stackoverflow.com/a/52101/497418). Hence the dupehammer. – zzzzBov Feb 27 '17 at 21:31
  • 2
    @zzzzBov Unnecessary dupe-hammer... OP is not asking how a for loop works, they are asking what it is doing within the context of the surrounding code.. – mhodges Feb 27 '17 at 21:31
  • 1
    That's what I'm talking about. @zzzzBov Please, do not abuse your rights to close questions. This one isn't really worth to be closed. – kind user Feb 27 '17 at 21:33
  • I'm providing a detailed answer. w8 – behkod Feb 27 '17 at 21:33
  • 1
    @zzzzBov it answers it assuming you're not confused by the usage in this particular question... which is the entire reason for the question. there's no indication the op doesn't understand for loops in general – aw04 Feb 27 '17 at 21:33
  • @mhodges, "OP is not asking how a for loop works" it's clear to me from reading the question that OP doesn't understand how a `for` loop works, and in gaining that understanding would no longer have the problem in the posted question. But, by all means, reopen if you feel otherwise. – zzzzBov Feb 27 '17 at 21:34
  • 1
    @zzzzBov The OP clearly explained that they understand how the structure of the `for` loop works via their "1. 2. 3." . I think the OP is more-so having issues with understanding unwrapping nested objects, which is what the code is actually doing. If you want to dupehammer with a question that explains that, feel free – mhodges Feb 27 '17 at 21:36
  • @mhodges, "The OP clearly explained that they understand how the structure of the for loop works", given that their "1. 2. 3." are incorrect it clearly shows that they *don't* understand how a `for` loop works. – zzzzBov Feb 27 '17 at 21:37
  • 1
    @zzzzBov You are missing the point. It's not about 'how for loop' works. It's about a **specific** mechanism in this **specific** case. I'm in this business since one year and it's the first time I see such case and solution, actually. – kind user Feb 27 '17 at 21:40
  • I understand how for loops work. I'm asking within the context of this code. – Danniel Rolfe Feb 27 '17 at 21:58
  • @BehradKhodayar You can add your answer now. – kind user Feb 27 '17 at 22:16

1 Answers1

3
for(var node = list; node; node = node.rest)

First part (up to first ;) means; initialize node by setting it to list;

Second part means; keep looping until 'node' is null. This is because the 2nd part is actually an expression, you often see something like i<list.length but it doesn't have to involve lengths, it just has to be a boolean, true means keep going and false means stop. In Javascript you can just put the object variable for the expression, it means the same as node!=null

Third part means; after each loop set node equal to the result of node.rest. Where node.rest is another object to work through.

So the first time it loops, this is what you get

node = {value: 1, rest: { value: 2, rest: { value: 3, rest: null}}}

and it will push node.value (which is 1) on to array.

Second time it loops you get;

node = { value: 2, rest: { value: 3, rest: null}}

and it will push node.value (which is 2) on to array.

And so on until node.rest is null.

Jim W
  • 4,866
  • 1
  • 27
  • 43