默认情况下,
我正在开发一款刽子手游戏,我正在检查用户通过按下键猜测的字母是否在随机选择的短语中。但无论信是否真的在俗语中,它要么总是给我"是的,它在俗语中",要么"不是的,它不在谚语中"的答案。非常感谢您的帮助。谢谢。
这是我目前正在使用的代码。
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");
}
});
随机选择的
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);
String.fromCharCode(event.keyCode);
返回大写字母。因此,你永远不会匹配。
编辑:澄清,提供的keyCodes是大写字母,因此String.fromCharCode默认情况下总是返回大写字母。