简体   繁体   English

提示识别 javascript 刽子手游戏“选择的单词”中的字母

[英]getting the prompt to recognize the letters in a javascript hangman games "chosen word"

so i am trying to teach myself javascript and have decided to try and make a basic hangman game, as it seems to be a popular interview/test question.所以我正在尝试自学 javascript 并决定尝试制作一个基本的刽子手游戏,因为它似乎是一个流行的面试/测试问题。 My plan is to use Math.random to pick a select word out of a pool of words, and then use the prompt feature to find the letters of the word.我的计划是使用 Math.random 从单词池中选择一个单词,然后使用提示功能查找单词的字母。 Unfortunately, I have been unable to figure out how to get the prompt to recognize my input.不幸的是,我一直无法弄清楚如何获得提示以识别我的输入。 Any help would be appreciated.任何帮助,将不胜感激。

 var secretWords = ["batman", "Donkey kong", "ninja", "programming"]; var chosenWord = secretWords[Math.floor(Math.random() * secretWords.length)]; var guesses = 8; var letters = chosenWord.length var guess = prompt("GUESS A LETTER"); alert(chosenWord); for (var i = 0; i <= letters; i++) { letters[i] = chosenWord.substring(i, i++) } if (guess = i) { alert("nice") } else { alert("Wrong"); }

i is your loop variable.我是你的循环变量。 It will always be equal to the value of letters when the loop is finished looping.当循环完成循环时,它总是等于字母的值。 That is not what you want to compare against.那不是你想要比较的。 Instead, compare as you loop through the letters.相反,在循环字母时进行比较。

var secretWords = ["batman", "Donkey kong", "ninja", "programming"];
var chosenWord = secretWords[Math.floor(Math.random()*secretWords.length)];
var guesses = 8;
var letters = chosenWord.length
var guess = prompt("GUESS A LETTER");    

var found = false;
for (var i = 0; i < letters; i++){
    if (guess == chosenWord.substring(i, i + 1))
        found = true;
}
if (found) {
    alert("Nice")
}
else {
    alert("Wrong");
}

At the moment you are looping from 0 to, and with, the length of the chosen word.目前,您正在从0循环到所选单词的长度。 I assume that you try to check if the letter given is in the word that is selected, but the logic is unfortunately off.我假设您尝试检查给定的字母是否在所选单词中,但不幸的是逻辑不正确。

To check if a string contains a letter, or part of a word, you can use the String.includes() method.要检查字符串是否包含字母或单词的一部分,您可以使用String.includes()方法。 Every string has this method and with it you can check if the given string is included in your string that compare it with.每个字符串都有这个方法,你可以用它检查给定的字符串是否包含在与它进行比较的字符串中。 It will then return true or false based on if the string is included or not.然后它将根据是否包含字符串返回 true 或 false。 For example:例如:

var example = 'hello';
example.includes('h'); // true
example.includes('i'); // false

JavaScript is case sensitive. JavaScript 区分大小写。 So in order for your game to work and make the user input ambiguous about uppercase or lowercase characters, make sure to make all your secretWords in either lowercase or uppercase, depending on your choice.因此,为了让您的游戏正常运行并使用户输入的大写或小写字符模糊不清,请确保根据您的选择将所有secretWords设为小写或大写。 Then when the user provides input, change the input to, again, either uppercase or lowercase with the String.toUpperCase() and String.toLowerCase() methods.然后,当用户提供输入时,再次使用String.toUpperCase()String.toLowerCase()方法将输入更改为大写或小写。

 var secretWords = ["batman", "donkey kong", "ninja", "programming"]; var chosenWord = secretWords[Math.floor(Math.random()*secretWords.length)]; var guesses = 8; var letters = chosenWord.length alert(chosenWord); var guess = prompt("GUESS A LETTER"); var guessLowerCase = guess.toLowerCase(); var isGuessedLetterInWord = chosenWord.includes(guessLowerCase); if (isGuessedLetterInWord) { alert('nice'); } else { alert('wrong'); }

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

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