I'm creating hangman game with JavaScript. Keycode is the number equivalent of letters in the console log.
If I want to use just [az] alphabet keys I have to set as between 65 and 90.
But I also have non-English letters.
Keycodes for non-English letters are = [186, 191, 219, 220,221]. How can I include these letters?
window.addEventListener('keydown', e => {
// console.log(e.keyCode);
if(e.keyCode >= 65 && e.keyCode <=90) {
const letter = e.key;
console.log(e.keyCode)
if (selectedWord.includes(letter)) {
if(!correctLetters.includes(letter)) {
correctLetters.push(letter);
displayWord();
} else {
showNotification();
}
} else {
if(!wrongLetters.includes(letter)) {
wrongLetters.push(letter);
updateWrongLettersEl();
} else {
showNotification();
}
}
}
});
You can use array.indexOf(value)
, which will search the array for the value and return the position of the found value. If it doesn't find the value, you'll get -1
.
// set up an array of the keys
var nonEnglishKeys = [186, 191, 219,220,221];
// then in your if statement, check for indexOf()
// if it doesn't equal -1, that means it's one of your keys
if((e.keyCode >= 65 && e.keyCode <=90) || nonEnglishKeys.indexOf(e.keyCode) !== -1) {
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.