Page 1 of 1
Shuffle / Randomize Array items in JavaScript
Posted: 13 Aug 2015, 14:00
by Marius
Hello
How can I shuffle /randomize an array in JavaScript? Is there a JavaScript function like shuffle() in PHP?
Shuffle / Randomize Array items in JavaScript
Posted: 13 Aug 2015, 14:09
by Admin
Hi
You can use this function:
Code: Select all
// receive an array and return it with the items shuffled /randomized
function shuffle(ar){
for(var j, x, i = ar.length; i; j = Math.floor(Math.random() * i), x = ar[--i], ar[i] = ar[j], ar[j] = x);
return ar;
}
- If you want to see an example and to test it, visit this page:
https://coursesweb.net/javascript/shuffl ... e-array_cs