I found such functions in nete:

function shuffle( array ) { // Shuffle an array for(var j, x, i = array.length; i; j = parseInt(Math.random() * i), x = array[--i], array[i] = array[j], array[j] = x); return true; } function shuffle(a) { for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [a[i], a[j]] = [a[j], a[i]]; } return a; } 

Could you explain how the loop without a body works? (from the first function) and what the string [a[i], a[j]] = [a[j], a[i]]; means [a[i], a[j]] = [a[j], a[i]]; ? or give a link

  • one
    it is enough to read about what a for loop is and what parameters it takes - Alexey Shimansky
  • 2
    [a[i], a[j]] = [a[j], a[i]] swapped the elements of the array with indices i and j . - Visman

1 answer 1

How does a bodyless cycle work?

as usual for :

 for ([начало]; [условие]; [постусловие]) выражения 
  1. The начало expression is executed, if specified. This expression usually initializes one or several counters, but the syntax allows you to write an expression of any complexity . Also used to declare variables.

    In this case, the variables j, x и i initialized.

  2. The условие is условие . If the условие true, the выражения are executed. If it is false, the for loop terminates. If the условие completely omitted, then it is considered true.

  3. The выражения are выражения . To execute multiple expressions, the block expression { ... } to group expressions.

    (!!!) If you put a semicolon immediately after for(); this is equivalent to empty for() {} brackets

  4. Постусловие - here you can also write an expression of any complexity , which will be executed at the end of the iteration. But usually they write a step there, for example i++ , but since this is done at the end of each iteration , then no one bothers to write down what the выражение could be in the block (in curly braces)

what does the string [a [i], a [j]] = [a [j], a [i]];

This is a second link destructurization that came with ES6

In general, a lot of good chips appeared in ES6 . So you need to get acquainted with this.

  • one
    Unstructured understood - analog php list ($ var1, $ var2, $ var3) = explode (",", $ string). but the second loop argument is simply i, and the first one is also i, then the loop will execute only 1 time? if so, how does the whole array mix up in one pass? - axmed2004
  • one
    read carefully - SECOND argument, the condition for continuation - axmed2004
  • one
    @ user202854 I read carefully, but you don’t)) What does the answer in paragraph 2 say ?? what is considered truth and what is not? and what happens in a --i with --i ? Answer yourself to these questions .............. in general, to figure out how it works, I would recommend putting braces after for yet, writing down any test construct, for example var test = 1; and take up debugging to sort through - Alexey Shimansky
  • one
    @ user202854 about debugging: ru.stackoverflow.com/a/701140/191482 - Alexey Shimansky