I'm in the process of building a hangman game. I'm attempting to capture a user guess with document.onkeyup and then splice that item from an array. If the user made the right guess, that is. Here is my function:
我正在建立一个刽子手游戏。我试图用document.onkeyup捕获用户猜测,然后从数组中拼接该项。如果用户做出了正确的猜测,那就是。这是我的功能:
var alphabet = ["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"];
var i = alphabet.indexOf;
document.onkeyup = function(event) {
console.log("event=", event);
var userGuess = String.fromCharCode(event.which).toLowerCase();
if (userGuess === alphabet[1] || alphabet[2] || alphabet[3]) {
alphabet.splice(i, 1 || 2 || 3);
console.log(alphabet);
}
};
Is this possible? Or do I have to add many more lines of code to achieve same desired effect, as I know how to splice and push, just can't seem to figure out if this is possible? I've been googling for quite some time and only seem to find answers to much more complex issues. I know this is an elementary issue, but I could really use the help - please and thank you.
这可能吗?或者我必须添加更多代码行来实现相同的预期效果,因为我知道如何拼接和推送,似乎无法弄清楚这是否可能?我一直在谷歌上搜索一段时间,似乎只能找到更复杂问题的答案。我知道这是一个基本问题,但我真的可以使用这个帮助 - 请谢谢。
1 个解决方案
#1
2
You could try following solution. If user hits the button and the given word includes that specified character, remove it from the alphabet
array. Feel free to modify it as you wish.
您可以尝试以下解决方案。如果用户点击按钮并且给定的单词包含指定的字符,请将其从字母数组中删除。随意修改它你想要的。
var words = "One apple a day",
alphabet = ["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"];
document.addEventListener('keyup', function(e) { //listen to the keyboard events
if (e.keyCode > 64 && e.keyCode < 91 && alphabet.indexOf(e.key) > -1) { //to avoid spam with logs, we will restrict the range of keycodes from 64 to 91 (a-z) ===> a keycode is 65, z is 90
if (words.indexOf(e.key) > -1) { //if clicked letter is included inside the words
alphabet.splice(alphabet.indexOf(e.key), 1); //then remove it from the alphabet
console.log('correct letter');
} else {
console.log('incorrect letter');
}
console.log(JSON.stringify(alphabet)); //show the alphabet and its actual state
}
});
#1
2
You could try following solution. If user hits the button and the given word includes that specified character, remove it from the alphabet
array. Feel free to modify it as you wish.
您可以尝试以下解决方案。如果用户点击按钮并且给定的单词包含指定的字符,请将其从字母数组中删除。随意修改它你想要的。
var words = "One apple a day",
alphabet = ["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"];
document.addEventListener('keyup', function(e) { //listen to the keyboard events
if (e.keyCode > 64 && e.keyCode < 91 && alphabet.indexOf(e.key) > -1) { //to avoid spam with logs, we will restrict the range of keycodes from 64 to 91 (a-z) ===> a keycode is 65, z is 90
if (words.indexOf(e.key) > -1) { //if clicked letter is included inside the words
alphabet.splice(alphabet.indexOf(e.key), 1); //then remove it from the alphabet
console.log('correct letter');
} else {
console.log('incorrect letter');
}
console.log(JSON.stringify(alphabet)); //show the alphabet and its actual state
}
});