压缩JavaScript随机语句生成器代码
我正在尝试创建一个生成器,它创建一个半随机的句子,基于从随机确定索引的不同单词数组中提取变量,然后从数组中删除该单词以确保没有重复 它是有效的,但不是以一种容易构建的方式。每次我想从已经从同一行中提取的数组中提取时,脚本就会停止压缩JavaScript随机语句生成器代码,javascript,random,Javascript,Random,我正在尝试创建一个生成器,它创建一个半随机的句子,基于从随机确定索引的不同单词数组中提取变量,然后从数组中删除该单词以确保没有重复 它是有效的,但不是以一种容易构建的方式。每次我想从已经从同一行中提取的数组中提取时,脚本就会停止 document.getElementById(“按钮”).onclick=function(){ genContent(); }; 函数genContent(){ var-content=“”; 列表(); //---什么有效--- content+=r(perso
document.getElementById(“按钮”).onclick=function(){
genContent();
};
函数genContent(){
var-content=“”;
列表();
//---什么有效---
content+=r(person).concat(“,r(动词),“ed”);
content+=r(person.concat(“,so”);
content+=r(person).concat(“is”,r(动词),“ing”);
内容+=r(人);
//---我想把它浓缩成什么---
//content+=r(person).concat(“,r(动词),“ed”,r(person),”,so),r(person),“is”,r(动词),“ing”,r(person));
document.getElementById(“输出”).innerHTML=content.charAt(0.toUpperCase()+content.slice(1);
};
函数r(数组){
random=Math.floor(Math.random()*array.length);
值=数组[随机];
阵列拼接(随机,1);
返回值;
};
函数列表(){
person=[“Grace”、“Jared”、“Suzy”、“Tommy”];
动词=[
“回答”、“询问”、“阻止”、“呼叫”,
“延迟”、“期望”、“跟随”、“问候”,
“帮助”、“通知”、“加入”、“踢”,
“标签”、“标记”、“需要”、“订单”,
“拾取”、“问题”、“请求”、“信号”,
“欺骗”、“访问”、“警告”];
};代码>
单击按钮生成一个句子。
只是concat把这件事弄得一团糟吗?您可以这样做:
content = r(person) + " " + r(verb) + "ed " + r(person) + ", so "
+ r(person) + " is " + r(verb) + "ing " + r(person);
您还可以使用数组连接,这有点不错,因为您可以在元素之间插入任何想要的字符,并且可以使用push()来构建数组
content = [r(person), " ", r(verb), "ed ", r(person), "
, so ", r(person), " is ", r(verb), "ing ", r(person)];
content = content.join("");
document.querySelector(“按钮”).addEventListener(“单击”,函数)(){
var person=[“Grace”、“Jared”、“Suzy”、“Tommy”];
变量动词=[“回答”、“询问”、“阻止”、“呼叫”、“延迟”、“期望”、“跟随”、“问候”、“帮助”、“通知”、“加入”、“踢”、“标签”、“标记”、“需要”、“命令”、“挑选”、“问题”、“请求”、“信号”、“戏法”、“拜访”、“警告”];
document.getElementById(“output”).innerHTML=[r(person),r(动词)+“ed”,r(person)+“so”,r(person),“is”,r(动词)+“ing”,r(person)+“]”。join(“”);
});
功能r(列表){
return list.splice(Math.floor(Math.random()*list.length),1)[0];
};代码>
去
另一种方法是使用模板,例如:
"[P] [V]ed [P], so [P] is [V]ing [P]"
并使用带有自定义回调函数的.replace()
方法:
var-person=[
“格蕾丝”、“杰瑞德”、“苏西”、“汤米”
],
动词=[
“回答”、“询问”、“阻止”、“呼叫”,
“延迟”、“期望”、“跟随”、“问候”,
“帮助”、“通知”、“加入”、“踢”,
“标签”、“标记”、“需要”、“订单”,
“拾取”、“问题”、“请求”、“信号”,
“把戏”、“拜访”、“警告”
];
var template=“[P][V]ed[P],因此[P]是[V]ing[P]”;
var str=template.replace(/\[([PV])\]/g,函数(m,p0){
变量列表={'P':人称,'V':动词}[p0];
返回list.splice((Math.random()*list.length)| 0,1);
});
console.log(str)代码>您的代码的不同方法/实现,就像输入一样:
//takes an Array, shuffles it in place (no copy), and returns the shuffled Array
function shuffle(arr){
for(var len = arr.length, i=len, j, tmp; i--; ){
j = Math.floor(Math.random() * (len-1));
if(j>=i) ++j;
tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
return arr;
}
//a utility to uppercase only the first-char of a string
function uppercaseFirstChar(str){
return str.charAt(0).toUpperCase() + str.slice(1);
}
//stringpool,
//the code never mutates this, so there's no need to ever reset it.
var strings = {//TODO: find a better name(space) than `strings`
persons: [
"Grace", "Jared", "Suzy", "Tommy"
],
verbs: [
"answer", "ask", "block", "call",
"delay", "expect", "follow", "greet",
"help", "inform", "join", "kick",
"label", "mark", "need", "order",
"pick", "question", "request", "signal",
"trick", "visit", "warn"
]
}
//keep the tasks simple and clear.
//this function builds and returns a random sentence.
//no more, no less.
//it doesn't need to know what the result is used for
function randomSentence(){
//don't mutate the string-pool, create a copy of the Arrays, and shuffle that
var person = shuffle( strings.persons.slice() );
var verb = shuffle( strings.verbs.slice() );
//build the sentence, uppercase the first char, and return the result
return uppercaseFirstChar(
`${person[0]} ${verb[0]}ed ${person[1]}, so ${person[2]} is ${verb[1]}ing ${person[3]}`
};
//an example how you can reference the same person/verb multiple times in the same result
//your r(array)-approach is not able to that; it's kind of destructive.
//return `${person[0]} ${verb[0]}ed ${person[1]}; ${person[1]} has been ${verb[0]}ed by ${person[0]}`
};
document.getElementById("button").onclick = function() {
document.getElementById("output").innerHTML = randomSentence();
};
实际上,您可以加入空格字符,这将有助于您清理,甚至不需要一些仅为空空格的数组条目!