Tsuna
Tsuna

Reputation: 2198

never seen for loop this way how does this work?

I am just reading through a book but it didn't mention using for this way. The most common way I can understand but this I have no idea how it start and end.

this is a function which has a for loop in it and what's being inserted into the argument is an object which inherits objects and objects like a list.

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

var object = {
    value: 10,
    rest: {
        value: 20,
        rest: {
            value: 30,
            rest: null
        }
    }
}

console.log(listToArray(object));
// → [10, 20, 30]

for (var node = list; node; node = node.rest) this is the part where I don't understand how it works. How it start or end.

I just have a simple guess myself to start off, node is an object so if node != null then node = list and node = node.rest which is the next object

Upvotes: 2

Views: 130

Answers (4)

Anonymous
Anonymous

Reputation: 1

Try this :

Boolean(object.rest); //true
Boolean(object.rest.rest); //true
Boolean(object.rest.rest.rest); //false

Upvotes: 0

Morklympious
Morklympious

Reputation: 1095

This is actually, really interesting! But what you have here is a for loop used in a pretty creative way to iterate deeper into an object.

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

If we think about for loops in a traditional sense, something like

for (var i = 0; i < 10; i++)

We can draw a few parallels.

If you look at the MDN for a for loop, you see that it's broken up into three sections:

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

Initialization -- Specify variables, etc that will be used at the start of the loop, in this case our starting node is equal to the list passed in.

Condition -- Now here's something a little different, but basically this middle statement is condition to determine if the loop should execute. Most of the time you see i < 10 or some numeric comparison because that's a common use case. This example is leveraging the truthiness of objects in javascript. That is, an object, taken by itself evaluates to true.

ex. if({}) // will be true The fact that node is placed there by itself is basically saying "Look at the truthiness of node, if the object exists, then let's run the loop! Otherwise node will be undefined, and undefined is a falsey value in javascript.

Lastly we have the final expression, or what I like to call the 'update' section. We're essentially assigning node to be equal to its child property, which means we're iteratively going deeper and deeper into these objects. if node.rest exists, then the next iteration of the loop will start there and run itself again if node.rest actually existed in the previous loop.

Good question. I enjoyed that one.

Upvotes: 1

podostro
podostro

Reputation: 86

Like in any other c based language, for loop is basically formed this way: for(initialization;condition;incrementation)

In this code, the initialization part is defining a new var named node, and initializing it´s value to the same as the argument list.

The condition part node in javascript means, while node != null (or unassigned)

The incrementation part is setting node to the value of node.rest. I dunno what node.rest does, but I suppose it will move to the next node in the tree/graph.

So in this case, the for starts when list is assigned, and ends when node.rest returns null/unassigned.

Upvotes: 1

Quentin
Quentin

Reputation: 943142

A for loop take three arguments:

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

var node = list; runs when the loop is started.

node is the condition, if it is false, the loop won't go around again.

node = node.rest is run at the end of the loop.

So:

It sets node to list and then keeps replacing node with the value of the previous nodes rest property.

Eventually node is { value: 30, rest: null }, so it sets node to null then tests if null is true and then stops because it isn't.

Upvotes: 5

Related Questions