And for a loop without any {}

So, I read about array shuffling. And then I came across this script :

shuffle = function(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

When I look carefully, forit doesn’t have at all {}! But it works like magic. I am very curious to know how this works. (and a bunch of commas too.)

+5
source share
6 answers

What follows for ()can be any statement; it can be something with curly braces, or it can be one expression, or it can be an empty expression. for (...);equivalently for (...) {}. Naturally, this should only be used in conjunction with a closed loop that will naturally end, or you will have an infinite loop on your hands.

- ; - , for ( , ).

for (
     // initialisation: declare three variables
     var j, x, i = o.length;
     // The loop check: when it gets to ``!i``, it will exit the loop
     i;
     // the increment clause, made of several "sub-statements"
     j = parseInt(Math.random() * i),
     x = o[--i],
     o[i] = o[j],
     o[j] = x
)
    ; // The body of the loop is an empty statement

:

for (
     // initialisation: declare three variables
     var j, x, i = o.length;
     // The loop check: when it gets to ``!i``, it will exit the loop
     i;
     // note the increment clause is empty
) {
     j = parseInt(Math.random() * i);
     x = o[--i];
     o[i] = o[j];
     o[j] = x;
}

while :

var j, x, i = o.length;
while (i) {
     j = parseInt(Math.random() * i);
     x = o[--i];
     o[i] = o[j];
     o[j] = x;
}
+10

{}, ; ( ), , for. .

  for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);


  for(var j, x, i = o.length;// Initialization
  i;// Work until i is zero
 j = parseInt(Math.random() * i),
  x = o[--i], o[i] = o[j], o[j] = x);//Here he is    computing his logic
+4

-, , , . .

, , for(...) . , . , :

for (...)
    foo;  // expression statement

for(...)
    {
       // block statement
    }

for(...)
   if(...) // If statement
      foo;

, ,

for (...)
    ;

";" . , , for(...);.

, . , ( ), . "" , , . , JS, for. .

, :

shuffle = function(o) {
    var j, x, i = o.length;
    while (i) { // for-loops are just while-loops in disguise
        j = parseInt(Math.random() * i), // even better: replace , by ;
        x = o[--i],
        o[i] = o[j],
        o[j] = x;
    }
    return o;
};

, x = o[--i] i--; x = o[i].

+2

, . i++ - . , . , for.

for (first section; second section; third section);

. .

.

, . , , ... , , , , .

+1

, .

for.

So the for loop has three parts

for (initial variables; end cases; what to do every iteration)

You define some source data and use the oone that was passed to the function, define the final register, and then compute something at each iteration. At the end, ohas a new meaning and returns.

+1
source

All work is performed within the parens operator for. There is no explicit body for the loop, so ;at the end it just says that the body is empty.

The operator ,(comma) evaluates expressions from left to right and returns the value of the rightmost one.

The loop is basically equivalent to:

for(var j, x, i = o.length; i > 0;)
{
    j = parseInt(Math.random() * i--);
    x = o[i];
    o[i] = o[j];
    o[j] = x
} 
0
source

All Articles