[英]Javascript string array ordering (based on value order)
I am currently using a typewriter effect from Codepen and would like to order strings in the order in which they are listed (not random).我目前正在使用Codepen的打字机效果,并希望按照列出的顺序(不是随机的)对字符串进行排序。
function getRandomPun() {
const puns = [
"String one",
"String two",
"String three"
];
const index = Math.floor(Math.random() * puns.length);
return puns[index];
}
startType(getRandomPun(), 0);
I have tried replacing the index with Math.floor(Math.round(puns.length)
but it has no effect.我尝试用
Math.floor(Math.round(puns.length)
替换索引,但没有效果。
function getThePun() {
const puns = [
"String one",
"String two",
"String three"
];
const index = Math.floor(Math.round(puns.length);
return puns[index];
}
startType(getThePun(), 0);
I also tried the following but only the first string it outputted:我还尝试了以下但只尝试了它输出的第一个字符串:
function getThePun() {
const puns = [
"String one",
"String two",
"String three"
];
return puns[0];
}
startType(getThePun(), 0);
Any help or tips would be greatly appreciated.任何帮助或提示将不胜感激。
Not sure what the usecase is here, but here is how to sort不确定这里的用例是什么,但这里是如何排序
const puns = [ "String shortest", "String - the longest", "String three long" ]; function getRandomPun(len) { if (len) puns.sort((a,b) => a.length - b.length) console.log(puns) const index = Math.floor(Math.random() * puns.length); return puns[index]; } console.log(getRandomPun()); console.log(getRandomPun(true));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.