我正在编写一个hangman风格的程序,但在检查是否输入了多个字符以及是否猜到了设定的单词时遇到了问题



我使用while循环来检查猜测次数是否小于10,一旦进行了10次猜测,它将最后一次提示您输入整个答案,而不仅仅是单个字符。我知道你可以使用.length来检查输入的长度,但我不知道如何检查特定的字符输入。我已经尝试过做";(guess.length===短语(";它是左边的提示变量和右边的单词变量。

这是我的密码。

function myGame()
{
var name = prompt("Please enter your name");
document.getElementById("user_name").innerHTML = "Welcome " + name + " to Wheel of Fortune";    
const d = new Date();
document.getElementById("today_date").innerHTML = d;    
var count = 0;
phrase = "parseint";
var qs = document.querySelector("#hWord").addEventListener("keydown", qSelect);
var lgth = phrase.length; var checkWin = false; var correct_guess = false; var guess;
var numCols = lgth; new_word = ""; var crazyWord = ""; var new_word = ""; crzLgth = crazyWord.length;
for (var cols = 0; cols < numCols; cols++)
document.getElementById('wheel_game' + cols).innerHTML = ("/ ");
document.getElementById('hWord').innerHTML = "The word was " + (phrase);  
function qSelect()
{ 
correct_guess = false;
guess = prompt("Guess a letter");
for (var j = 0; j < lgth; j++)
{
if (guess === phrase.charAt(j))
{
correct_guess = true;
count = count + 1;  
document.getElementById('wheel_game' + j).innerHTML = guess;        
for (var nw = 0; nw < lgth; nw++)
crazyWord = crazyWord + (document.getElementById('wheel_game' + nw).innerHTML);        
}

new_word = crazyWord.substr((crzLgth - lgth), lgth);

checkWin = checkWord(phrase, new_word);      
}

if (checkWin === true)
{ 
document.getElementById("result").innerHTML = ("You win!");
}
else if (checkWin === false)
{ 
document.getElementById("result").innerHTML = ("You Lose");    
if (correct_guess === false)
count = count + 1;  
if (count === 10) 
{
guess = prompt("One last chance to guess the word");    
}
else if (guess.length === phrase) {
checkWin = true;
document.getElementById("result").innerHTML = ("You win!");
} 
}
function checkWord(phrase, otherWord)
{
var cleanWord;
cleanWord = otherWord;
if (phrase === cleanWord)
return true;
else
return false;
}
}
}

HTML。

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="Wheel of Fortune">
<title>Wheel of Fortune Game</title>
</head>
<body>
<h1>Welcome player</h1>    
<p> Click to begin </p>
<p>
<input type="button" value="Begin" onclick="myGame();" />    
</p>    
<p id="user_name"> </p> <br>
<p id="today_date"> </p> <br>
<p id="catagory"> </p> <br>
<div id="wheel_game0"></div> <br>
<div id="wheel_game1"></div> <br>
<div id="wheel_game2"></div> <br>
<div id="wheel_game3"></div> <br>
<div id="wheel_game4"></div> <br>
<div id="wheel_game5"></div> <br>
<div id="wheel_game6"></div> <br>
<div id="wheel_game7"></div> <br>
<div id="wheel_game8"></div> <br>
<div id="wheel_game9"></div> <br>

<div id="hWord"><p></p></div>
<div id="result"><p></p></div>

<script src="myScript.js"></script> 
</body>
</html>

while循环中的所有内容都应该在由触发的单独函数中

document.querySelector("#hWord").addEventListener("keydown", yourFunction);

而关键字应该删除

相关内容

  • 没有找到相关文章

最新更新