Below is the Flow chart which explains how array is shuffles or arranged in JavaScript.
Code:
function shuffle(arra1) {
var ctr = arra1.length, temp, index;
// While there are elements in the array
while (ctr > 0) {
// Pick a random index
index = Math.floor(Math.random() * ctr);
// Decrease ctr by 1
ctr--;
// And swap the last element with it
temp = arra1[ctr];
arra1[ctr] = arra1[index];
arra1[index] = temp;
}
return arra1;
}
var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log('Given Array','=','0,1,2,3,4,5,6,7,8,9',"\n");
console.log('Shuffled Array');
console.log(shuffle(myArray));
Output:
The Tech Platform
Comments