用空格替换逗号?Fisher-Yates 随机化 [英] Replace commas with spaces ? Fisher-Yates randomization

查看:46
本文介绍了用空格替换逗号?Fisher-Yates 随机化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢 @axtck 对 Fisher Yates 随机化的帮助,他帮我在这里将数字更改为单词:

由于洗牌函数会洗牌数组索引,因此您只需洗牌数组与您所做的相同,但在数组中添加名称字符串.

代码现在显示一串单词,单词之间用逗号分隔,效果很好!

现在我想用空格替换逗号(例如:Histoire Chien Koala Arbre Italique Lampadaire Docteur Boulet Maison Forge Gagnant Ennui)

有人可以帮我用空格更改这些逗号吗?

谢谢

//用名字替换数字const inputArray = [Arbre"、Boulet"、Chien"、Docteur"、Ennui"、Forge"、Gagnant"、Histoire"、Italique"、Koala"、Lampadaire"、Maison""];//定义Fisher-Yates shuffleconst fisherShuffle = 函数(数组){让 currentIndex = array.length,临时值,随机索引;while (0 !== currentIndex) {randomIndex = Math.floor(Math.random() * currentIndex);当前索引 -= 1;临时值 = 数组[当前索引];数组[当前索引] = 数组[随机索引];数组[随机索引] = 临时值;}document.getElementById("fyresults").append(array.toString());};fisherShuffle(inputArray);

<p><span id="fyresults"></span></p>

解决方案

对数组调用 toString() 将返回数组的字符串表示形式,即 item1,item2,....

如果你想以另一种方式加入数组,你可以使用数组方法join(),它需要一个分隔符字符串,在你的情况下是一个空格join(" ").

一些例子:

const testArr = ["first", "second", "..."];//示例数组const arrString = testArr.toString();//对其调用 toString()const arrJoinedX = testArr.join("X");//加入 Xconst arrJoinedSpace = testArr.join(" ");//按空格连接//记录console.log("数组:", testArr);console.log("Array toString():", arrString);console.log("数组加入 X:", arrJoinedX);console.log("数组加入空格:", arrJoinedSpace);

因此要将其应用于您的示例,您可以执行以下操作:

//用名字替换数字const inputArray = [Arbre"、Boulet"、Chien"、Docteur"、Ennui"、Forge"、Gagnant"、Histoire"、Italique"、Koala"、Lampadaire"、Maison""];//定义Fisher-Yates shuffleconst fisherShuffle = 函数(数组){让 currentIndex = array.length,临时值,随机索引;while (0 !== currentIndex) {randomIndex = Math.floor(Math.random() * currentIndex);当前索引 -= 1;临时值 = 数组[当前索引];数组[当前索引] = 数组[随机索引];数组[随机索引] = 临时值;}const joinBySpace = array.join(" ");//按空格连接document.getElementById("fyresults").append(joinedBySpace);//附加到元素};fisherShuffle(inputArray);

<p><span id="fyresults"></span></p>

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>

解决方案

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>

这篇关于用空格替换逗号?Fisher-Yates 随机化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆