使用JavaScript数组实现MadLib随机选词功能遇阻求助
解决MadLib随机选词功能问题
问题原因
直接将数组拼接到字符串时,JavaScript会自动把数组转为逗号分隔的完整字符串(比如nounArray会输出"pebbles,shoes,trees"),而非取出单个随机元素。你写的randomWord函数仅能打印单个随机词,但没有复用逻辑处理所有词类数组。
解决方案
- 编写通用随机词函数
改造原randomWord,让它支持传入任意数组并返回单个随机元素:
function getRandomWord(arr) { const randomIndex = Math.floor(Math.random() * arr.length); return arr[randomIndex]; }
- 修改
madlib2函数
调用通用函数获取每个词类的随机元素,再拼接到故事中(推荐用模板字符串${}替代加号拼接,更易维护):
function madlib2() { // 从对应数组获取随机词 const randomNoun = getRandomWord(nounArray); const randomNoun2 = getRandomWord(noun2Array); const randomNoun3 = getRandomWord(noun3Array); const randomVerb = getRandomWord(verbArray); const randomVerb2 = getRandomWord(verb2Array); const randomAdj = getRandomWord(adjectiveArray); const randomAdj2 = getRandomWord(adjective2Array); const randomAdj3 = getRandomWord(adjective3Array); // 拼接故事内容 let storyContent = `All of the ${randomNoun} and ${randomNoun2} were gone when he awoke, finding that the ${randomNoun3} now continued uninterrupted in their place. He tried to ${randomVerb} for help, but was ${randomAdj} to find that his mouth was ${randomAdj2}, the lower half of his face now ${randomAdj3}, continuous flesh. He tried to ${randomVerb2} his panic by closing his eyes and counting to ten. On the eleventh second of darkness, he realised his mistake.`; document.getElementById("content").innerHTML = storyContent; }
- 清理冗余代码
你定义的全局变量var noun = [];、var noun2 = [];等在madlib函数里被重新声明为局部变量,全局版本完全无用,可直接删除。
完整修正后的代码
var storyContent = ""; //story that will be displayed when run madlib(); //madlib function function madlib() { let wordform = document.getElementById("wordform"); //calls the form in the HTML let noun = document.getElementById("Noun").value; let noun2 = document.getElementById("Noun2").value; let noun3 = document.getElementById("Noun3").value; let verb = document.getElementById("Verb").value; let verb2 = document.getElementById("Verb2").value; let adjective = document.getElementById("Adjective").value; let adjective2 = document.getElementById("Adjective2").value; let adjective3 = document.getElementById("Adjective3").value; let storyContent = "All of the " + noun + " and " + noun2 + " were gone when he awoke, finding that the " + noun3 + " now continued uninterrupted in their place. He tried to " + verb + " for help, but was " + adjective + " to find that his mouth was " + adjective2 + ", the lower half of his face now " + adjective3 + ", continuous flesh. He tried to " + verb2 + " his panic by closing his eyes and counting to ten. On the eleventh second of darkness, he realised his mistake." document.getElementById("content").innerHTML = storyContent; } let generateBtn = document.getElementById("button"); generateBtn.addEventListener('click', madlib); const nounArray = ["pebbles", "shoes", "trees"]; let noun2Array = ["seats", "books", "houses"]; let noun3Array = ["balls", "games", "spatulas"]; let verbArray = ["bolt", "leap", "fall"]; let verb2Array = ["lay", "sing", "skate"]; let adjectiveArray = ["golden", "chosen", "eerie"]; let adjective2Array = ["cautious", "slim", "probable"]; let adjective3Array = ["wide", "ossified", "overjoyed"]; function getRandomWord(arr) { const randomIndex = Math.floor(Math.random() * arr.length); return arr[randomIndex]; } function madlib2() { const randomNoun = getRandomWord(nounArray); const randomNoun2 = getRandomWord(noun2Array); const randomNoun3 = getRandomWord(noun3Array); const randomVerb = getRandomWord(verbArray); const randomVerb2 = getRandomWord(verb2Array); const randomAdj = getRandomWord(adjectiveArray); const randomAdj2 = getRandomWord(adjective2Array); const randomAdj3 = getRandomWord(adjective3Array); let storyContent = `All of the ${randomNoun} and ${randomNoun2} were gone when he awoke, finding that the ${randomNoun3} now continued uninterrupted in their place. He tried to ${randomVerb} for help, but was ${randomAdj} to find that his mouth was ${randomAdj2}, the lower half of his face now ${randomAdj3}, continuous flesh. He tried to ${randomVerb2} his panic by closing his eyes and counting to ten. On the eleventh second of darkness, he realised his mistake.`; document.getElementById("content").innerHTML = storyContent; } let randomBtn = document.getElementById("button2"); randomBtn.addEventListener('click', madlib2); function showhide() { var div = document.getElementById("content"); div.classList.toggle('hidden'); var div = document.getElementById("end"); div.classList.toggle('hidden'); var div = document.getElementById("line"); div.classList.toggle('hidden'); }
内容的提问来源于stack exchange,提问作者WastelandSniper
相关产品推荐
相关产品推荐

