randomshufflerandomized-algorithmfisher-yates-shuffle

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>


Solution

  • Calling toString() on an array will return the string representation of the array which is item1,item2,....

    If you want to join the array in another way, you can use array method join(), it takes a separator string, which in your case would be a space join(" ").

    Some examples:

    const testArr = ["first", "second", "..."]; // example array
    
    const arrString = testArr.toString(); // calling toString() on it
    const arrJoinedX = testArr.join("X"); // joining by X
    const arrJoinedSpace = testArr.join(" "); // joining by space
    
    // logging
    console.log("Array:", testArr);
    console.log("Array toString():", arrString);
    console.log("Array joined with X:", arrJoinedX);
    console.log("Array joined with space:", arrJoinedSpace);

    So to apply this to your example, you can do:

    // 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;
      }
      
      const joinedBySpace = array.join(" "); // join by space
      document.getElementById("fyresults").append(joinedBySpace); // append to element
    };
    
    fisherShuffle(inputArray);
    <p><span id="fyresults"></span></p>