Javascript猜谜游戏,定义onkeyup变量
Javascript guessing game, defining onkeyup variable
javascript 超级新手。我正在尝试制作一个通灵猜谜游戏。除了 onkeyup 函数,这里的一切都有效。当我打开控制台日志并输入字母时,它告诉我 userGuess 变量未定义。如何定义 userGuess 变量以匹配 onkeyup 函数?
感谢:
//Available choices
var letterChoices = ['a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
//score
var wins = 0;
var losses = 0;
var guesses = 9;
var guessesLeft = 9;
var guessedLetters = [];
var letterToGuess = null;
//computer randomly chooses a letter
var computerGuess = letterChoices [Math.floor(Math.random()*letterChoices.length)];
//guesses left function
var updateGuessesLeft = function() {
document.querySelector('#guessLeft').innerHTML = "Guesses Left: " + guessesLeft;
};
//letter to guess function
var updateletterToGuess = function(){
this.letterToGuess = this.letterChoices[Math.floor(Math.random() * this.letterChoices.length)];
};
var updateGuessesSoFar = function(){
document.querySelector('#let').innerHTML = "Your guesses so far: " + guessedLetters.join(', ');
};
//reset
var reset = function(){
totalGuesses = 9;
guessesLeft = 9;
guessedLetters = [];
updateletterToGuess();
updateGuessesSoFar();
updateGuessesLeft();
};
updateGuessesLeft();
updateletterToGuess();
//user input key
document.onkeyup = function(event) {
guessesLeft--;
var userGuess;
console.log(userGuess)
guessedLetters.push(userGuess);
updateGuessesLeft();
updateGuessesSoFar();
if (guessesLeft > 0){
if (userGuess === letterToGuess){
wins++;
document.querySelector('#wins').innerHTML = 'Wins: ' + wins;
alert("How did you know!?!");
reset();
}
} else if (guessesLeft == 0){
losses++;
document.querySelector('#losses').innerHTML = 'Losses: ' + losses;
alert("Sorry, you're not a psychic!");
reset();
}
}
变量userGuess
是undefined
因为它刚刚被声明。这个变量没有发生赋值操作。我查看了工作示例,在浏览器中对其进行了修改,它似乎按预期工作。我做的唯一改变是 assign userGuess
到按下的键,如下
var userGuess = event.key;
这是我对您的代码进行一些尝试后得到的输出
既然你有一个你想要允许的字符列表,你可以在开头添加以下代码,以确保不考虑允许字符以外的任何字符
if (letterChoices.indexOf(userGuess) === -1) return;
javascript 超级新手。我正在尝试制作一个通灵猜谜游戏。除了 onkeyup 函数,这里的一切都有效。当我打开控制台日志并输入字母时,它告诉我 userGuess 变量未定义。如何定义 userGuess 变量以匹配 onkeyup 函数?
感谢:
//Available choices
var letterChoices = ['a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
//score
var wins = 0;
var losses = 0;
var guesses = 9;
var guessesLeft = 9;
var guessedLetters = [];
var letterToGuess = null;
//computer randomly chooses a letter
var computerGuess = letterChoices [Math.floor(Math.random()*letterChoices.length)];
//guesses left function
var updateGuessesLeft = function() {
document.querySelector('#guessLeft').innerHTML = "Guesses Left: " + guessesLeft;
};
//letter to guess function
var updateletterToGuess = function(){
this.letterToGuess = this.letterChoices[Math.floor(Math.random() * this.letterChoices.length)];
};
var updateGuessesSoFar = function(){
document.querySelector('#let').innerHTML = "Your guesses so far: " + guessedLetters.join(', ');
};
//reset
var reset = function(){
totalGuesses = 9;
guessesLeft = 9;
guessedLetters = [];
updateletterToGuess();
updateGuessesSoFar();
updateGuessesLeft();
};
updateGuessesLeft();
updateletterToGuess();
//user input key
document.onkeyup = function(event) {
guessesLeft--;
var userGuess;
console.log(userGuess)
guessedLetters.push(userGuess);
updateGuessesLeft();
updateGuessesSoFar();
if (guessesLeft > 0){
if (userGuess === letterToGuess){
wins++;
document.querySelector('#wins').innerHTML = 'Wins: ' + wins;
alert("How did you know!?!");
reset();
}
} else if (guessesLeft == 0){
losses++;
document.querySelector('#losses').innerHTML = 'Losses: ' + losses;
alert("Sorry, you're not a psychic!");
reset();
}
}
变量userGuess
是undefined
因为它刚刚被声明。这个变量没有发生赋值操作。我查看了工作示例,在浏览器中对其进行了修改,它似乎按预期工作。我做的唯一改变是 assign userGuess
到按下的键,如下
var userGuess = event.key;
这是我对您的代码进行一些尝试后得到的输出
既然你有一个你想要允许的字符列表,你可以在开头添加以下代码,以确保不考虑允许字符以外的任何字符
if (letterChoices.indexOf(userGuess) === -1) return;