vendredi 31 janvier 2020

shuffle string with javascript and keep track of the number

I have a function

function shuffle(length,string) {
   var result           = '';
   var characters       = string;
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
       result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   return result;
}

and I use it like to generate the array of the random string

var value = shuffle(66,'guitar'); value.split('')

Now I am using this function in a setinterval function to generate the random string every 5 seconds But I need to keep track of the characters so if the first characters was moved to fifth position I need to know.

so the output of the shuffle function should be

array('g,5','i,4','t,1')

Numbers after comma denoted the new place of the characters with respect to the index of the array.First position was moved to fifth and second position was moved to fourth position and so on.




Aucun commentaire:

Enregistrer un commentaire