所以,我正在研究一个随机句子生成器。我试图让它工作,但该功能似乎没有运行(这被保存为一个HTML文件,我在Firefox中运行它)。这是迄今为止的代码:
<html>
<body>
<h1>Hello.</h1>
<br>
<p>This is a website made by me. No, I do not own a domain. However, you can run this file from your computer. Thanks for looking at my website. Please enjoy!</p>
Hey! look at this button I made! I'm working on more stuff as you look at this.
<button onclick="GenerateSentence()">Click me for a random sentence!</button>
<p id="Output"></p>
<script>
function GenerateSentence() {
alert("test")
var subjectran = math.floor(math.random() * subject.length)
var verbran = math.floor(math.random() * verb.length)
var adjectiveran = math.floor(math.random() * adjective.length)
var objectran = math.floor(math.random() * object.length)
var adverbran = math.floor(math.random() * adverb.length)
var subject = ["Perry", "Obama", "Liam", "Eric", "Gregory", "Daniel", "Mikey", "Pog", "Benji", "Raj", "Jarec", "Theo", "Zach", "Nicholas", "Samir", "Ivan", "Colonel Sanders", "SK"]
var verb = ["eats", "hates", "loves", "dyes", "kills", "dreams about", "runs with", "deep-fries", "fries", "burns", "ignites", "texts", "harvests", "stores", "runs over", "buys", "sells", "explodes", "studies", "destroys", "breaks", "reks", "bodies", "mauls", "melts", "pops", "slices", "butchers", "has nightmares about", "has flashbacks about", "jars", "thinks about", "makes art with", "builds a house with", "writes with"]
var adjective = ["dank", "red", "orange", "yellow", "green", "blue", "indigo", "violet", "purple", "white", "black", "brown", "red-orange", "yellow-green", "dumb", "stupid", "smart", "", "rainbow", "titanium", "adamantium", "paper", "black and white", "sad", "happy", "angry", "flat", "winded", "donated", "edible", "fat", "slow", "fast", "energetic", "lazy", "hot", "cold", "dead"]
var object = ["toys", "cars", "Ferraris", "Mustangs", "Maseraties", "buses", "people", "heads", "blocks", "houses", "cheeses", "Teletubbies", "Gengis Kahns", "mustaches", "cows", "pigs", "chickens", "houses", "Nicholas Cages", "George Washingtons", "Kim Jong Uns", "calculators", "tables", "schools", "classes", "suits", "pencils", "fried chicken", "sheep", "moose", "sinks", "beans", "tomatoes", "peas", "pumpkins", "tater tots", "hotdogs", "lemons"]
var adverb = ["", "quickly", "often", "slowly", "carefully", "correctly", "eagerly", "easily", "fast", "loudly", "quietly", "patiently", "outside", "somewhere", "downstairs", "abroad", "underground", "upstairs", "anywhere", "accidentally", "intentionally", "seldom", "sometimes", "usually", "rarely", "here"]
ver sentence = (subject[subjectran] + " " + verb[verbran] + " " + adjective[adjectiveran] + " " + object[objectran] + " " + adverb[adverbran])
document.getElementById("Output").innerHTML = sentence
}
</script>
</body>
</html>
该代码段显示错误“missing; before statement”,但在firefox中运行时我没有遇到任何错误。记住,我是一个业余爱好者。我犯了错误,或者这应该有用吗?提前谢谢。
编辑:看了3条评论之后,我看到了一些我没看过的错误。此外,我没有意识到阵列不能正常工作。谢谢你的帮助!
答案 0 :(得分:0)
以下是包含所有更正的HTML:
<html>
<body>
<h1>Hello.</h1><br>
<p>This is a website made by me. No, I do not own a domain. However, you can run this file from your computer. Thanks for looking at my website. Please enjoy!</p>
Hey! look at this button I made! I'm working on more stuff as you look at this.<button onclick="generateSentence();">Click me for a random sentence!</button>
<p id="Output"></p>
<script>
function generateSentence(){
alert("test");
var subject = ["Perry","Obama","Liam","Eric","Gregory","Daniel","Mikey","Pog","Benji","Raj","Jarec","Theo","Zach","Nicholas","Samir","Ivan","Colonel Sanders","SK"];
var verb = ["eats","hates","loves","dyes","kills","dreams about","runs with","deep-fries","fries","burns","ignites","texts","harvests","stores","runs over","buys","sells","explodes","studies","destroys","breaks","reks","bodies","mauls","melts","pops","slices","butchers","has nightmares about","has flashbacks about","jars","thinks about","makes art with","builds a house with","writes with"];
var adjective = ["dank","red","orange","yellow","green","blue","indigo","violet","purple","white","black","brown","red-orange","yellow-green","dumb","stupid","smart","","rainbow","titanium","adamantium","paper","black and white","sad","happy","angry","flat","winded","donated","edible","fat","slow","fast","energetic","lazy","hot","cold","dead"];
var object = ["toys","cars","Ferraris","Mustangs","Maseraties","buses","people","heads","blocks","houses","cheeses","Teletubbies","Gengis Kahns","mustaches","cows","pigs","chickens","houses","Nicholas Cages","George Washingtons","Kim Jong Uns","calculators","tables","schools","classes","suits","pencils","fried chicken","sheep","moose","sinks","beans","tomatoes","peas","pumpkins","tater tots","hotdogs","lemons"];
var adverb = ["","quickly","often","slowly","carefully","correctly","eagerly","easily","fast","loudly","quietly","patiently","outside","somewhere","downstairs","abroad","underground","upstairs","anywhere","accidentally","intentionally","seldom","sometimes","usually","rarely","here"];
var subjectran = Math.floor(Math.random()*subject.length);
var verbran = Math.floor(Math.random()*verb.length);
var adjectiveran = Math.floor(Math.random()*adjective.length);
var objectran = Math.floor(Math.random()*object.length);
var adverbran = Math.floor(Math.random()*adverb.length);
var sentence = (subject[subjectran] + " " + verb[verbran] + " " + adjective[adjectiveran] + " " + object[objectran] + " " + adverb[adverbran]);
document.getElementById("Output").innerHTML = sentence;
}
</script>
</body>
</html>
您还应该考虑将这些数组移到函数之外,这样就不会继续初始化它们。