[英]Checking user input against an array, act as a guess Javascript
I am a new aspiring dev and I am trying to figure out how to build a game of hangman using vanilla js only. 我是一个有抱负的新开发人员,我试图弄清楚如何仅使用Vanilla JS构建子手游戏。 I have put together a key event listner, and have got it to console log the inputs.
我整理了一个关键事件列表器,并用它来控制台记录输入。 I have also got it to print the letters pushed into a "letters guessed" array.
我还可以打印推入“猜字母”数组中的字母。
document.addEventListener("keypress", letterPressed);
function letterPressed(event) {
var letter = String.fromCharCode(event.keyCode);
guessedLetters.push(letter);
document.getElementById("lettersGuessed").innerHTML = guessedLetters;
console.log(guessedLetters)
}
I also have an array of choices of words 我也有很多选择
var wordList = ["Ravens", "Cardinals", "Falcons", "Bills",
"Panthers", "Bears", "Bengals", "Browns", "Cowboys",
"Broncos", "Lions", "Packers", "Texans", "Colts",
"Jaguars", "Cheifs", "Chargers", "Rams",
"Dolphins", "Vikings", "Patriots", "Saints",
"Giants", "Jets", "Raiders", "Eagles", "Steelers",
"Forty Niners", "Seahawks", "Buccaneers", "Titans",
"Redskins"];
and a for loop picking the random word from this array, converting it to "_" strings in the length of the word, and printing it to the html document in a div id of "spaces". 和一个for循环,从该数组中选择随机单词,将其转换为单词长度的“ _”字符串,并以“ spaces”的div ID将其打印到html文档中。
var wordBlanks = [];
var guessedLetters = [];
var randomWord = wordList[Math.floor(Math.random() * wordList.length)];
for (var i = 0; i < randomWord.length; i++) {
wordBlanks[i] = "_";
console.log(wordBlanks,randomWord);
document.getElementById("spaces").innerHTML = wordBlanks.join(" ");
};
Where would I even want to go from here? 我什至想从这里去哪里? I want to check input from the keystrokes (or the letters guessed array, im not sure which would be best) against the word thats chosen and have the "_" strings reveal the correct guesses when guessed correctly.
我想对照选择的单词检查击键(或字母猜测的数组,我不确定哪一个最好)的输入,并在正确猜测时让“ _”字符串显示正确的猜测。
My question is more regarding pointing me in the right direction. 我的问题更多是指向正确的方向。 So, I can properly teach myself.
因此,我可以适当地自学。 Any words of advice?
有什么建议吗?
Thank you! 谢谢!
You'll have to come to terms with the upper/lowercase issue first, but after that, something like this will work: 您必须首先解决大小写问题,但是之后,类似的事情将起作用:
const randomWord = "BEARS"; const guessedLetters = ["S", "O", "E"]; const wordBlanks = randomWord.split('') .map(letter => guessedLetters.indexOf(letter) >= 0 ? letter : "_") .join(' '); console.log(wordBlanks);
Instead of putting into the guessedLetters
array, try to see if the presses letter can be found in the randomWord
. 而不是放到
guessedLetters
数组中,请尝试看看是否可以在randomWord
找到印刷机字母。
If so, find the position of the letter and let it replace the appropriate _
space in the wordBlank
array. 如果是这样,找到字母的位置,并让它替换
wordBlank
数组中的_
空格。
Something like: 就像是:
function letterPressed(event) {
var letter = String.fromCharCode(event.keyCode);
if (randomWord.indexOf(letter) >= 0)
wordBlank[(randomWord.indexOf(letter)] = letter;
console.log(wordBlank);
}
Note that a letter may have multiple occurences in a word. 请注意,一个字母在一个单词中可能多次出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.