无法在刽子手

时间:2018-05-05 23:48:27

标签: javascript

我试图为一个项目制作一个刽子手游戏。该网站要求该人员猜测一个数字,然后填写该单词的空白。我遇到的一个问题是,我可以在最后输入带有数字的单词,这是我不想要的。

所以,基本上我可以输入" Something12"这将有效。我无法弄清楚如何让它在我保存的数组中搜索数字。我不确定我需要编码以查找和删除或重新提示用户输入有效字词。



/* Global Variables */
var guessWord = [];
var guessWord1 = [];
var underScores = [];
var letters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";

/* Function for the Guess word, gets the word and puts in the underscores */
function gWord()
{
	// Asks for the word and removes punctuation 
	guessWord = prompt("Please input the guess word below");
	guessWord1 = guessWord.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");   
	
	// If it is not a word, will re-prompt for an actual word
	while(guessWord1 == "" || guessWord1 == null || isNaN(guessWord1) == false)	
	{
		var guessWord2 = guessWord1.search(letters);
		alert(guessWord2);
		
		if(guessWord1 != letters)
			{
				alert("This is not a valid word, please try again");
				guessWord = prompt("Please input the guess word below");
				guessWord1 = guessWord.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,""); 
			} 
	}
	
		for(var i = 0; i < guessWord1.length; i++)
			{
				underScores.push("_");
			}  
		document.getElementById("replace").innerHTML = underScores;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="author" content="" />
<meta name="Keywords" content="" />

<style type="text/css">

body {
width: 1280px;
background-color: grey; }

#contain {
height: 500px;
width: 1000px;
background-color: lightblue; 
border: 5px blue groove;
margin: auto; }

#replace {
width: 900px;
height: 125px;
border: 5px blue groove;
background-color: white;
position: absolute;
top: 350px;
left: 200px; }

#image {
border: 5px blue groove;
background-color: white;
position: absolute;
top: 50px;
left: 200px;
height: 250px;
width: 250px; }

#incorrect {
border: 5px blue groove;
background-color: white;
position: absolute;
top: 50px;
left: 600px;
height: 125px;
width: 400px; }

#Wrong {
position: absolute;
left: 110px;
top: -12px;
}

#button {
border: 5px blue groove;
border-radius: 15px;
background-color: white;
position: absolute;
top: 200px;
left: 700px;
height: 50px;
width: 200px; 
font-size: 20px; 
cursor: pointer; }

#bText {
position: absolute;
left: 37px;
top: -12px;
}

#button:active {
left: 702px;
top: 202px; }

</style>

<script type="text/JavaScript" src="main.js">
</script>

</head>
<body onload= "gWord();">
<div id="contain">

<div id="replace">

</div>

<div id="image">


</div>
<div id="incorrect">
<h2 id="Wrong"><u>Incorrect Letters</u></h2>
<span id="wList">Hi</span>
</div>

<div id="button">
<h4 id="bText">Enter a Letter</h4>
</div>


</div>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

假设您现有的正则表达式正在运行,向其中添加\d也会删除数字。

guessWord1 = guessWord.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()\d]/g,"");   

更新了更好的方法来通知用户,而不是简单地删除不需要的字符。

我还添加了一个检查是否按下Cancel,以及是否退出游戏。如果您不想这样,只需删除行if (!guessWord) { return false; }

即可

Stack snippet

/* Global Variables */
var guessWord = [];
var guessWord1 = [];
var underScores = [];

/* Function for the Guess word, gets the word and puts in the underscores */
function gWord() {
  // Asks for the word and removes punctuation 
  guessWord = prompt("Please input the guess word below");

  // If it is not a word, will re-prompt for an actual word
  while ((!guessWord || guessWord.length < 1 || !/^[a-zA-Z]+$/.test(guessWord))) {
  
      // If cancel, exit game
      if (!guessWord) { return false; }
      
      alert("This is not a valid word, please try again");
      guessWord = prompt("Please input the guess word below");
  }
  
  for (var i = 0; i < guessWord.length; i++) {
    underScores.push("_");
  }
  document.getElementById("replace").innerHTML = underScores;
}
<!DOCTYPE html>
<html>

<head>
  <title></title>
  <meta name="author" content="" />
  <meta name="Keywords" content="" />

  <style type="text/css">
    body {
      width: 1280px;
      background-color: grey;
    }
    
    #contain {
      height: 500px;
      width: 1000px;
      background-color: lightblue;
      border: 5px blue groove;
      margin: auto;
    }
    
    #replace {
      width: 900px;
      height: 125px;
      border: 5px blue groove;
      background-color: white;
      position: absolute;
      top: 350px;
      left: 200px;
    }
    
    #image {
      border: 5px blue groove;
      background-color: white;
      position: absolute;
      top: 50px;
      left: 200px;
      height: 250px;
      width: 250px;
    }
    
    #incorrect {
      border: 5px blue groove;
      background-color: white;
      position: absolute;
      top: 50px;
      left: 600px;
      height: 125px;
      width: 400px;
    }
    
    #Wrong {
      position: absolute;
      left: 110px;
      top: -12px;
    }
    
    #button {
      border: 5px blue groove;
      border-radius: 15px;
      background-color: white;
      position: absolute;
      top: 200px;
      left: 700px;
      height: 50px;
      width: 200px;
      font-size: 20px;
      cursor: pointer;
    }
    
    #bText {
      position: absolute;
      left: 37px;
      top: -12px;
    }
    
    #button:active {
      left: 702px;
      top: 202px;
    }
  </style>

  <script type="text/JavaScript" src="main.js">
  </script>

</head>

<body onload="gWord();">
  <div id="contain">

    <div id="replace">

    </div>

    <div id="image">


    </div>
    <div id="incorrect">
      <h2 id="Wrong"><u>Incorrect Letters</u></h2>
      <span id="wList">Hi</span>
    </div>

    <div id="button">
      <h4 id="bText">Enter a Letter</h4>
    </div>


  </div>
</body>

</html>