[英]Scrambling a word in javascript
I want to make a system which scrambles word, example: Hello
-> llHoe
.我想制作一个打乱单词的系统,例如:
Hello
-> llHoe
。 The problem is that it sometimes shows undefined
in the console and I can't find any problems.问题是它有时在控制台中显示
undefined
,我找不到任何问题。
This is the code I've made:这是我制作的代码:
function GenerateWord() { var Words = ["Hello", "Bye", "Tree"] var RandomNumber = Math.floor((Math.random() * Words.length)); var CorrectAwnserString = Words[RandomNumber]; var CorrectAwnser = CorrectAwnserString.split(""); var WordToOrder = ""; for(i = CorrectAwnser.length; i > 0;) { let RandomLetter = Math.floor((Math.random() * i)); WordToOrder = WordToOrder + CorrectAwnser[RandomLetter]; console.log(WordToOrder) CorrectAwnser.splice(RandomLetter); console.log(CorrectAwnser) i = i - 1; } document.getElementById("OriginalWord").innerHTML = CorrectAwnserString; document.getElementById("MessedWord").innerHTML = WordToOrder; } GenerateWord();
<div id="OriginalWord"></div> <div id="MessedWord"></div>
The error seems to be with how you are splicing the string.错误似乎与您拼接字符串的方式有关。 You need to specify how many elements should be removed.
您需要指定应删除多少个元素。 I shuffled your code around a little and stuck it in some more console logs it looks like this:
我把你的代码打乱了一点,把它放在更多的控制台日志中,它看起来像这样:
for(i = CorrectAwnser.length; i > 0;) {
let RandomLetter = Math.floor((Math.random() * i));
WordToOrder = WordToOrder + CorrectAwnser[RandomLetter];
console.log(CorrectAwnser,RandomLetter,CorrectAwnser[RandomLetter]);
console.log(WordToOrder)
CorrectAwnser.splice(RandomLetter); // should be CorrectAwnser.splice(RandomLetter, 1);
i = i - 1;
}
console.log({CorrectAwnserString,WordToOrder});
The code above gave the following result:上面的代码给出了以下结果:
[ 'T', 'r', 'e', 'e' ] 1 r
r
[ 'T' ] 0 T
rT
[] 0 undefined
rTundefined
[] 0 undefined
{ CorrectAwnserString: 'Tree', WordToOrder: 'rTundefinedundefined' }
After updating the splice statement to CorrectAwnser.splice(RandomLetter, 1);
将 splice 语句更新为
CorrectAwnser.splice(RandomLetter, 1);
the logs are the following:日志如下:
[ 'T', 'r', 'e', 'e' ] 3 e
e
[ 'T', 'r', 'e' ] 0 T
eT
[ 'r', 'e' ] 0 r
eTr
[ 'e' ] 0 e
eTre
{ CorrectAwnserString: 'Tree', WordToOrder: 'eTre' }
If you write CorrectAwnser.splice(2)
, it will remove 2 elements, not the 2nd element.如果您编写
CorrectAwnser.splice(2)
,它将删除 2 个元素,而不是第二个元素。 That's why your splicing is removing more than one element, and sometimes the CorrectAwnser
becomes empty before your loop ends.这就是为什么您的拼接删除了多个元素,并且有时在循环结束之前
CorrectAwnser
变为空的原因。
You should write你应该写
CorrectAwnser.splice(RandomLetter, 1)
You can use String.prototype
method as it will return an array of the string ( Cool huh no need to generate one ! ) .您可以使用
String.prototype
方法,因为它将返回一个字符串数组(很酷,不需要生成一个!)。 Also to randomise the string characters you can use Math.random
function in a specific way ...同样要随机化字符串字符,您可以以特定方式使用
Math.random
函数......
See =>见 =>
String.prototype.randomize = function() {
let array = Array.from(this).map((val, index) => this[index]);
array.sort(() => 0.5 - Math.random());
const string = array.reduce((string, word) => string += word, "");
return string;
};
let str = "Overflow";
str = str.randomize();
console.log(str);
You can use this method on any string datatype.. so I think you can handle it now
你可以在任何字符串数据类型上使用这个方法..所以我认为你现在可以处理它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.