Javascript 我希望这段代码不会太长,也不会太耗时

Javascript 我希望这段代码不会太长,也不会太耗时,javascript,Javascript,我希望能够继续开发这段代码,但编写这段代码要花很多时间。有没有办法让它更简单?我的意思是,写起来花更少的时间更容易 <div id="jesus"> </div> <script>var a = ["The","an","a"] var b = ["deranged","squishy","original","excellent","extraordinary","big nosed","big bird lookin","angry"] var

我希望能够继续开发这段代码,但编写这段代码要花很多时间。有没有办法让它更简单?我的意思是,写起来花更少的时间更容易

    <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>

变量a=[“The”,“an”,“a”]
变量b=[“精神错乱”、“黏糊糊”、“原创”、“优秀”、“非凡”、“大鼻子”、“大鸟看”、“愤怒”]
变量c=[“猫”、“虾”、“西拉斯”、“龙虾”、“艾萨克”、“阿提克斯”、“莫莉”、“豪尔赫”、“多项式”]
动词变量=[“飞”、“跳”、“死”、“沸腾”]
var con=[“and”,“but”,“or”]
var prep=[“上方”、“打开”、“关闭”、“下方”]
var op=[“桥”、“房子”、“章鱼”、“秋千”、“船”]
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;
如果(rana==0){tobea=a[0]
}else如果(rana==1){tobea=a[1]
}else如果(rana==2){tobea=a[2]}
如果(ranb==0){tobeb=b[0]
}else如果(ranb==1){tobeb=b[1]
}else如果(ranb==2){tobeb=b[2]}
else如果(ranb==3){tobeb=b[3]}
else如果(ranb==4){tobeb=b[4]}
else如果(ranb==5){tobeb=b[5]}
else如果(ranb==6){tobeb=b[6]}
else如果(ranb==7){tobeb=b[7]}
如果(ranc==0){tobec=c[0]
}else如果(ranc==1){tobec=c[1]
}else如果(ranc==2){tobec=c[2]}
else如果(ranc==3){tobec=c[3]}
else如果(ranc==4){tobec=c[4]}
else如果(ranc==5){tobec=c[5]}
else如果(ranc==6){tobec=c[6]}
else如果(ranc==7){tobec=c[7]}
else如果(ranc==8){tobec=c[8]}
如果(rand==0){tobed=a[0]}
else如果(rand==1){tobed=a[1]}
如果(rand==0){tobee=a[0]}
else如果(rand==1){tobee=a[2]}
如果(ranverb==0){tobeverb=动词[0]
}else如果(ranverb==1){tobeverb=动词[1]
}else如果(ranverb==2){tobeverb=动词[2]}
else如果(ranverb==3){tobeverb=动词[3]}
如果(rancon==0){tobecon=con[0]
}如果(rancon==1){tobecon=con[1]
}如果(rancon==2){tobecon=con[2]}
如果(ranprep==0){tobeprep=prep[0]
}else如果(ranprep==1){tobeprep=prep[1]
}else如果(ranprep==2){tobeprep=prep[2]}
else如果(ranprep==3){tobeprep=prep[3]}
如果(ranop==0){tobeop=op[0]
}else如果(ranop==1){tobeop=op[1]
}else如果(ranop==2){tobeop=op[2]}
else如果(ranop==3){tobeop=op[3]}
else如果(ranop==4){tobeop=op[4]}
如果(ranat==0){tobeat=at[0]
}else如果(ranat==1){tobeat=at[1]
}else如果(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}
如果(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输出=tobea+“”+tobeb+“”+tobec+“”+tobeverb+“”+tobeprep+“”+tobeat+“”+tobeop;
document.getElementById(“jesus”).textContent=输出;
document.getElementById(“clickMe”).onclick=reload;

这就是所有的代码。

我总能看到我的重复试图达到什么目的

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可以以不同的方式使用,因为它非常通用。重构时,始终尽可能多地编写泛型函数

您可能需要考虑使用一些字符串和/或数组方法,如
indexOf
includes
来缩短
if…charAt
codeall t