Bläddra i källkod

swap 2 var without another temp one

Skylsmoi 6 år sedan
förälder
incheckning
7d6ab70239
1 ändrade filer med 6 tillägg och 8 borttagningar
  1. 6 8
      jsonserver/server.js

+ 6 - 8
jsonserver/server.js Visa fil

@@ -6,19 +6,17 @@ const middlewares = jsonServer.defaults()
6 6
 const GLOBAL_PORT = 3001
7 7
 
8 8
 function shuffle(array) {
9
-  var currentIndex = array.length, temporaryValue, randomIndex;
9
+  let currentIndex = array.length
10
+  let randomIndex
10 11
 
11 12
   // While there remain elements to shuffle...
12
-  while (0 !== currentIndex) {
13
-
13
+  while (currentIndex !== 0) {
14 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 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 22
   return array;