JSFiddle - React, Tailwind, and code Playground
JavaScript
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;
}
function shuffle2(array,aux){
var i;
var keeper = [];
for(i=0;i<aux.length;i++){
keeper[i]=array[aux[i]];
}
for(i=aux.length-1;i>=0;i--){//Its important to do this from the last to the first
array.splice(aux[i],1);//this will remove the index element shifting the rest of elemnts
}
shuffle(array);
//now we put back the elements we took out.
for(i=0;i<aux.length;i++){
array.splice(aux[i],0,keeper[i]);//this will remove the index element shifting the rest of elemnts
}
return array;
}
a = [1,2,3,4,5]
shuffle2(a, [1, 3])
document.body.innerHTML = JSON.stringify(a);