Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

shuffling in js

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

// Used like so
var arr = [2, 11, 37, 42];
shuffle(arr);
console.log(arr);
Comment

PREVIOUS NEXT
Code Example
Javascript :: gatsby-plugin-create-client-paths 
Javascript :: javascript get name of element 
Javascript :: json stringify indent 
Javascript :: how to add sticky function in javascript 
Javascript :: prevent browser back button jquery 
Javascript :: how to get day from javascript date 
Javascript :: email validator javascript 
Javascript :: phone number validation regex 
Javascript :: javascript local storage delete 
Javascript :: remove duplicate object from array 
Javascript :: display image base64 in REACT NATIVE 
Javascript :: loopback get relationship in before save 
Javascript :: H. Nazmul Hassan 
Javascript :: @viewchild elementref 
Javascript :: moment js difference between start and end in hours 
Javascript :: retrieve domain from email address node js 
Javascript :: javascript array find highest value of array of objects by key 
Javascript :: javascript get timestamp 
Javascript :: javascript persistent storage 
Javascript :: python get json content from file 
Javascript :: page reload button using angular 
Javascript :: nodejs create buffer from string 
Javascript :: javascript sort array alphabetically 
Javascript :: redirect via javascript 
Javascript :: vehicle number regex 
Javascript :: array sort by key javascript 
Javascript :: props to react router link 
Javascript :: inner content 
Javascript :: opencv4nodejs mac install 
Javascript :: match word in string js 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =