Thanks to @axtck for the help for the Fisher Yates randomization, he helped me to change number into words here :
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>
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 spacejoin(" ")
.Some examples:
So to apply this to your example, you can do: