我希望能够继续开发此代码,但需要花费大量时间来编写代码。有关如何使其更容易的任何想法?通过花更少的时间来写,我的意思更容易。
<div id="jesus">
</div>
<script>var a = ["The","an","a"]
var b = ["deranged","squishy","original","excellent","extraordinary","big nosed","big bird lookin","angry"]
var c = ["cat","shrimp","silas","lobster","isaac","atticus","molly","jorge","POLYNOMIAL"]
var verb = ["flew","jumped","died","boinged"]
var con = ["and","but","or"]
var prep = ["over","on","off","under"]
var op = ["bridge","house","octopus","swing","boat"]
var at = ["The","an","a"]
var rana = Math.floor(Math.random() * 2) + 0;
var ranb = Math.floor(Math.random() * 7) + 0;
var ranc = Math.floor(Math.random() * 8) + 0;
var rand = Math.floor(Math.random() * 2) + 0;
var ranverb = Math.floor(Math.random() * 3) + 0;
var rancon = Math.floor(Math.random() * 2) + 0;
var ranprep = Math.floor(Math.random() * 3) + 0;
var ranop = Math.floor(Math.random() * 4) + 0;
var ranat = Math.floor(Math.random() * 2) + 0;
var tobea = 0;
var tobeb = 0;
var tobec = 0;
var tobed = 0;
var tobee = 0;
var tobeverb = 0;
var tobecon = 0;
var tobeprep = 0;
var tobeop = 0;
var tobeat =0;
if(rana === 0) {tobea = a[0]
}else if(rana === 1) {tobea = a[1]
}else if(rana === 2) {tobea = a[2]}
if(ranb === 0) {tobeb = b[0]
}else if(ranb === 1) {tobeb = b[1]
}else if(ranb === 2) {tobeb = b[2]}
else if(ranb === 3) {tobeb = b[3]}
else if(ranb === 4) {tobeb = b[4]}
else if(ranb === 5) {tobeb = b[5]}
else if(ranb === 6) {tobeb = b[6]}
else if(ranb === 7) {tobeb = b[7]}
if(ranc === 0) {tobec = c[0]
}else if(ranc === 1) {tobec = c[1]
}else if(ranc === 2) {tobec = c[2]}
else if(ranc === 3) {tobec = c[3]}
else if(ranc === 4) {tobec = c[4]}
else if(ranc === 5) {tobec = c[5]}
else if(ranc === 6) {tobec = c[6]}
else if(ranc === 7) {tobec = c[7]}
else if(ranc === 8) {tobec = c[8]}
if(rand === 0) {tobed = a[0]}
else if(rand === 1) {tobed = a[1]}
if(rand === 0) {tobee = a[0]}
else if(rand === 1) {tobee = a[2]}
if(ranverb === 0) {tobeverb = verb[0]
}else if(ranverb === 1) {tobeverb = verb[1]
}else if(ranverb === 2) {tobeverb = verb[2]}
else if(ranverb === 3) {tobeverb = verb[3]}
if(rancon === 0) {tobecon = con[0]
}else if(rancon === 1) {tobecon = con[1]
}else if(rancon === 2) {tobecon = con[2]}
if(ranprep === 0) {tobeprep = prep[0]
}else if(ranprep === 1) {tobeprep = prep[1]
}else if(ranprep === 2) {tobeprep = prep[2]}
else if(ranprep === 3) {tobeprep = prep[3]}
if(ranop === 0) {tobeop = op[0]
}else if(ranop === 1) {tobeop = op[1]
}else if(ranop === 2) {tobeop = op[2]}
else if(ranop === 3) {tobeop = op[3]}
else if(ranop === 4) {tobeop = op[4]}
if(ranat === 0) {tobeat = at[0]
}else if(ranat === 1) {tobeat = at[1]
}else if(ranat === 2) {tobeat = at[2]}
if(tobeb.charAt(0) === "a") {tobea = tobed}
if(tobeb.charAt(0) === "e") {tobea = tobed}
if(tobeb.charAt(0) === "i") {tobea = tobed}
if(tobeb.charAt(0) === "o") {tobea = tobed}
if(tobeb.charAt(0) === "u") {tobea = tobed}
if(tobeb.charAt(0) === "b") {tobea = tobee}
if(tobeb.charAt(0) === "c") {tobea = tobee}
if(tobeb.charAt(0) === "d") {tobea = tobee}
if(tobeb.charAt(0) === "f") {tobea = tobee}
if(tobeb.charAt(0) === "g") {tobea = tobee}
if(tobeb.charAt(0) === "h") {tobea = tobee}
if(tobeb.charAt(0) === "j") {tobea = tobee}
if(tobeb.charAt(0) === "k") {tobea = tobee}
if(tobeb.charAt(0) === "l") {tobea = tobee}
if(tobeb.charAt(0) === "m") {tobea = tobee}
if(tobeb.charAt(0) === "n") {tobea = tobee}
if(tobeb.charAt(0) === "p") {tobea = tobee}
if(tobeb.charAt(0) === "q") {tobea = tobee}
if(tobeb.charAt(0) === "r") {tobea = tobee}
if(tobeb.charAt(0) === "s") {tobea = tobee}
if(tobeb.charAt(0) === "t") {tobea = tobee}
if(tobeb.charAt(0) === "v") {tobea = tobee}
if(tobeb.charAt(0) === "w") {tobea = tobee}
if(tobeb.charAt(0) === "x") {tobea = tobee}
if(tobeb.charAt(0) === "y") {tobea = tobee}
if(tobeb.charAt(0) === "z") {tobea = tobee}
if(tobeop.charAt(0) === "a") {tobeat = tobed}
if(tobeop.charAt(0) === "e") {tobeat = tobed}
if(tobeop.charAt(0) === "i") {tobeat = tobed}
if(tobeop.charAt(0) === "o") {tobeat = tobed}
if(tobeop.charAt(0) === "u") {tobeat = tobed}
if(tobeop.charAt(0) === "b") {tobeat = tobee}
if(tobeop.charAt(0) === "c") {tobeat = tobee}
if(tobeop.charAt(0) === "d") {tobeat = tobee}
if(tobeop.charAt(0) === "f") {tobeat = tobee}
if(tobeop.charAt(0) === "g") {tobeat = tobee}
if(tobeop.charAt(0) === "h") {tobeat = tobee}
if(tobeop.charAt(0) === "j") {tobeat = tobee}
if(tobeop.charAt(0) === "k") {tobeat = tobee}
if(tobeop.charAt(0) === "l") {tobeat = tobee}
if(tobeop.charAt(0) === "m") {tobeat = tobee}
if(tobeop.charAt(0) === "n") {tobeat = tobee}
if(tobeop.charAt(0) === "p") {tobeat = tobee}
if(tobeop.charAt(0) === "q") {tobeat = tobee}
if(tobeop.charAt(0) === "r") {tobeat = tobee}
if(tobeop.charAt(0) === "s") {tobeat = tobee}
if(tobeop.charAt(0) === "t") {tobeat = tobee}
if(tobeop.charAt(0) === "v") {tobeat = tobee}
if(tobeop.charAt(0) === "w") {tobeat = tobee}
if(tobeop.charAt(0) === "x") {tobeat = tobee}
if(tobeop.charAt(0) === "y") {tobeat = tobee}
if(tobeop.charAt(0) === "z") {tobeat = tobee}
var output = tobea + " " + tobeb + " " + tobec + " " + tobeverb + " " + tobeprep + " " + tobeat + " " + tobeop;
document.getElementById("jesus").textContent = output;
document.getElementById("clickMe").onclick = reload;
</script>
这是所有代码。
答案 0 :(得分:0)
我总能看到我的重复尝试实现的目标
1)从数组中获取随机单词
2)检查是否有元音以元音开头
可以这样表达
const getRandom = (arr) => arr[(Math.floor(Math.random() * arr.length -1) + 0)] //following your code not sure if this is correct random
const startsWithVowel = (str)=>/^[aeiou]/.test(str)
然后你的代码将缩短到这一点。
var a = ["The","an","a"]
var b = ["deranged","squishy","original","excellent","extraordinary","big nosed","big bird lookin","angry"]
var c = ["cat","shrimp","silas","lobster","isaac","atticus","molly","jorge","POLYNOMIAL"]
var verb = ["flew","jumped","died","boinged"]
var con = ["and","but","or"]
var prep = ["over","on","off","under"]
var op = ["bridge","house","octopus","swing","boat"]
var at = ["The","an","a"]
const getRandom = (arr) => arr[(Math.floor(Math.random() * arr.length -1) + 0)]
const startsWithVowel = (str)=>/^[aeiou]/.test(str)
let tobea = getRandom(a),
tobeb = getRandom(b),
tobec = getRandom(c),
tobed = a[getRandom([0,1])],//get 'The' or 'a'
tobee = a[getRandom([0,2])],//get 'The' or 'an'
tobeverb =getRandom(verb),
tobecon = getRandom(con),
tobeprep = getRandom(prep),
tobeop = getRandom(op),
tobeat = getRandom(at);
tobeat = startsWithVowel(tobeop) ? tobed : tobee
tobeb = startsWithVowel(tobeb) ? tobed: tobee
var output = tobea + " " + tobeb + " " + tobec + " " + tobeverb + " " + getRandom(prep) + " " + tobeat + " " + getRandom(op);
document.getElementById("jesus").textContent = output;
document.getElementById("clickMe").onclick = reload;
您可以看到getRandom可以以不同的方式使用,因为它非常通用。重构时,请尽可能多地编写泛型函数。