mardi 8 juin 2021

Replace commas with spaces ? Fisher-Yates randomization

Thanks to @axtck for the help for the Fisher Yates randomization, he helped me to change number into words here :

Since the shuffle functions shuffle the arrays indexes, you can just shuffle the array the same way you did but add name strings in the array.

The code is now showing a string of words with commas as separation between the words, its working well!

Now I want to replace commas, with spaces (example : Histoire Chien Koala Arbre Italique Lampadaire Docteur Boulet Maison Forge Gagnant Ennui)

Can somebody help me to change these commas with blank spaces?

Thanks

   // replace numbers with names   
const inputArray = ["Arbre", "Boulet", "Chien", "Docteur", "Ennui", "Forge", "Gagnant", "Histoire", "Italique", "Koala", "Lampadaire", "Maison"];


//define Fisher-Yates shuffle
const fisherShuffle = function(array) {
  let currentIndex = array.length,
    temporaryValue,
    randomIndex;

  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
  document.getElementById("fyresults").append(array.toString());
};

fisherShuffle(inputArray);
<p><span id="fyresults"></span></p>



Aucun commentaire:

Enregistrer un commentaire