Skip to content

Instantly share code, notes, and snippets.

@akirattii
Created March 9, 2017 03:42
Show Gist options
  • Save akirattii/d71312789c8650b434942c3c5c067ff7 to your computer and use it in GitHub Desktop.
Save akirattii/d71312789c8650b434942c3c5c067ff7 to your computer and use it in GitHub Desktop.
Shuffles array using pure js
/**
* Shuffles array
* @param {Array} - array
*/
function shuffle(a) {
let len = a.length;
for (let i = len; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
let arr = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
shuffle(arr);
console.log(arr);
/*
Console log:
["7", "2", "8", "1", "5", "3", "9", "4", "6"]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment