在我的猜字母函数中,我在单词变量的子字符串和长度方法中遇到错误,因为它说它未定义但是在这里明确定义
var wordsArray = ["monitor", "program", "application", "keyboard", "javascript", "gaming", "network"];
var word;
var goesLeft;
var placeholder;
var input;
var wordLength;
var wordSubstring;
为了解决这个问题,我尝试为word.Substring和word.Length创建变量,并在函数中定义它们的值,但我仍然没有任何东西
这是猜字母的功能
function guessLetter()
{
var correct = 0;
var inputBox = document.getElementById("guessinput");
input = inputBox.value;
console.log(word);
for (var count = 0; count < wordLength; count++)
{
if (input == word.substring(count, count + 1))
{
correct++;
placeholder = placeholder.substring(0, count) + input + placeholder.substring(count + 1, placeholder.length + 1);
document.getElementById("placeholder").innerHTML = placeholder;
}
}
if (correct == 0)
{
goesLeft--;
}
var url = document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman" + goesLeft + ".jpg";
if (placeholder == word)
{
document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman_win.jpg";
alert("You guessed the word correctly.You win!");
}
if (goesLeft == 0)
{
alert("You lose");
newGame();
}
}
错误即将到来
if (input == word.substring(count, count + 1))
以及定义for循环的地方
错误显示“在事件期间检测到错误:无法调用未定义的方法'子字符串'”
有什么想法吗? 非常感谢提前
答案 0 :(得分:0)
根据您发布的代码,您永远不会将字词设置为值。
var word; <-- that is undefined
代码中的某处我希望看到
currentWord = 0;
word = wordsArray[currentWord];