简体   繁体   English

我怎样才能让我的程序检查所选单词中是否有按下的键

[英]How can I get my program to check if a pressed key is in a selected word

I'm working on a hangman game and I'm checking to see if the letter that the user guesses by the use of key down is in the randomly chosen saying. 我正在做一个hang子手游戏,正在检查用户是否通过按下按键猜出的字母是否在随机选择的说法中。 But it either always gives me the answer of "Yes it is in the saying" or "No it is not in the saying" no matter if the letter is actually in the saying or not. 但这总是给我答案:“是的在说”或“否,不是在说”,无论这封信实际上是否在说。 Any help much appreciated. 任何帮助,不胜感激。 Thankyou. 谢谢。

Here is the code I'm working with at the moment. 这是我目前正在使用的代码。

document.addEventListener("keydown", function textFunction(event)
{
if (event.keyCode > 64 && event.keyCode < 91) 
{
    var guess = event.keyCode;
    var a = "a";
    var letterGuess = String.fromCharCode(event.keyCode);
    if(sayings[randomSaying].contains(guess)){
        alert("yes");
    } else {
        alert("no");
    }
    alert(String.fromCharCode(event.keyCode));
} 
else 
{
    alert("Please type a letter");
}
});

And the randomly chosen saying 和随机选择的说法

var sayings = [
    "cash on the nail",
    "charley horse",
    "double cross",
    "fit as a fiddle",
    "hands down",
    "if the cap fits",
    "mumbo jumbo",
    "see red",
    "stone the crows",
    "thick and thin",
]
sayings.toString();
var randomSaying = Math.floor(Math.random()*sayings.length);

The String.fromCharCode(event.keyCode); String.fromCharCode(event.keyCode); returns capital letter by default. 默认情况下返回大写字母。 As such, you'll never match. 因此,您将永远无法比拟。

EDIT: Clarification, the keyCodes supplied are the capitalized letter, so the String.fromCharCode will always return a capital letter by default. 编辑:澄清,提供的keyCodes为大写字母,因此默认情况下String.fromCharCode始终返回大写字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM