shuffle json array javascript

Solutions on MaxInterview for shuffle json array javascript by the best coders in the world

showing results for - "shuffle json array javascript"
Niklaus
14 Jan 2018
1function shuffle(arra1) {
2    var ctr = arra1.length, temp, index;
3
4// While there are elements in the array
5    while (ctr > 0) {
6// Pick a random index
7        index = Math.floor(Math.random() * ctr);
8// Decrease ctr by 1
9        ctr--;
10// And swap the last element with it
11        temp = arra1[ctr];
12        arra1[ctr] = arra1[index];
13        arra1[index] = temp;
14    }
15    return arra1;
16}
17var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
18console.log(shuffle(myArray));