JavaScript单词打乱游戏校验功能异常排查:点击检查始终提示错误的原因及修复方案
问题排查与修复方案
嘿,我帮你找到了两个关键问题,正是它们导致点击「检查」按钮时无论输入对错都提示错误:
1. toLowerCase方法未正确调用
在initGame函数里,你给correctWord赋值时犯了一个小疏忽:
correctWord = randomObj.word.toLowerCase;
toLowerCase是字符串的方法,必须加上括号()才能执行并返回小写的单词字符串。如果不加括号,correctWord存储的是这个方法的函数引用,而不是实际的单词内容。这样用户输入的字符串永远不可能和一个函数引用相等,自然会一直提示错误。
2. 输入空值的判断顺序错误
在checkWord函数里,你先判断了userWord !== correctWord,之后才判断空输入的情况。这会导致用户没输入内容时,先触发"答案不正确"的提示,然后才弹出"请输入单词"的提示,逻辑顺序完全颠倒了。
修复后的完整JavaScript代码
const wordText = document.querySelector(".word"), hintText = document.querySelector(".hint span"), inputField = document.querySelector(".input"), refreshBtn = document.querySelector(".refresh-word"), checkBtn = document.querySelector(".check-word"); // Words/Kelimeler const words = [ { word: "addition", hint: "The process of adding numbers", }, { word: "meeting", hint: "Event in which people come together", }, { word: "number", hint: "Math symbol used for counting", }, { word: "exchange", hint: "The act of trading", }, { word: "canvas", hint: "Piece of fabric for oil painting", }, { word: "garden", hint: "Space for planting flower and plant", }, { word: "position", hint: "Location of someone or something", }, { word: "feather", hint: "Hair like outer covering of bird", }, { word: "comfort", hint: "A pleasant feeling of relaxation", }, { word: "tongue", hint: "The muscular organ of mouth", }, { word: "expansion", hint: "The process of increase or grow", }, { word: "country", hint: "A politically identified region", }, { word: "group", hint: "A number of objects or persons", }, { word: "taste", hint: "Ability of tongue to detect flavour", }, { word: "store", hint: "Large shop where goods are traded", }, { word: "field", hint: "Area of land for farming activities", }, { word: "friend", hint: "Person other than a family member", }, { word: "pocket", hint: "A bag for carrying small items", }, { word: "needle", hint: "A thin and sharp metal pin", }, { word: "expert", hint: "Person with extensive knowledge", }, { word: "statement", hint: "A declaration of something", }, { word: "second", hint: "One-sixtieth of a minute", }, { word: "library", hint: "Place containing collection of books", }, ]; let correctWord; const initGame = () => { let randomObj = words[Math.floor(Math.random() * words.length)]; let wordArray = randomObj.word.split(""); for (let i = wordArray.length - 1; i > 0; i--) { let j = Math.floor(Math.random() * (i + 1)); let temp = wordArray[i]; wordArray[i] = wordArray[j]; wordArray[j] = temp; } wordText.innerText = wordArray.join(""); hintText.innerText = randomObj.hint; correctWord = randomObj.word.toLowerCase(); // 修复:添加括号调用方法 console.log(randomObj); }; const checkWord = () => { let userWord = inputField.value.toLowerCase(); // 改用更通用的toLowerCase() if (!userWord) return alert(`Please enter word`); // 修复:优先判断空输入 if (userWord !== correctWord) return alert(`Oops! ${userWord} is not correct word`); alert(`Congrats! ${userWord.toUpperCase()} is correct`); inputField.value = ""; // 可选优化:正确后清空输入框,提升体验 }; refreshBtn.addEventListener("click", initGame); checkBtn.addEventListener("click", checkWord); initGame();
另外提个小细节:你之前用了toLocaleLowerCase(),这个方法会根据地区规则转换小写,一般场景下用toLowerCase()就足够通用啦,当然如果你的游戏有特定地区需求也可以保留。
内容的提问来源于stack exchange,提问作者Ahmet Enes Bahar
相关产品推荐
相关产品推荐

