Procházet zdrojové kódy

swap 2 var without another temp one

Skylsmoi před 7 roky
rodič
revize
7d6ab70239
1 změnil soubory, kde provedl 6 přidání a 8 odebrání
  1. 6 8
      jsonserver/server.js

+ 6 - 8
jsonserver/server.js Zobrazit soubor

6
 const GLOBAL_PORT = 3001
6
 const GLOBAL_PORT = 3001
7
 
7
 
8
 function shuffle(array) {
8
 function shuffle(array) {
9
-  var currentIndex = array.length, temporaryValue, randomIndex;
9
+  let currentIndex = array.length
10
+  let randomIndex
10
 
11
 
11
   // While there remain elements to shuffle...
12
   // While there remain elements to shuffle...
12
-  while (0 !== currentIndex) {
13
-
13
+  while (currentIndex !== 0) {
14
     // Pick a remaining element...
14
     // Pick a remaining element...
15
-    randomIndex = Math.floor(Math.random() * currentIndex);
16
-    currentIndex -= 1;
15
+    randomIndex = Math.floor(Math.random() * currentIndex)
16
+    currentIndex -= 1
17
 
17
 
18
     // And swap it with the current element.
18
     // And swap it with the current element.
19
-    temporaryValue = array[currentIndex];
20
-    array[currentIndex] = array[randomIndex];
21
-    array[randomIndex] = temporaryValue;
19
+    ;[array[randomIndex], array[currentIndex]] = [array[currentIndex], array[randomIndex]]
22
   }
20
   }
23
 
21
 
24
   return array;
22
   return array;