未识别变量的问题

时间:2016-08-10 19:00:51

标签: javascript html

我正在制作一个刽子手游戏,并且已经被HTML,CSS和JS困扰了。我有以下内容,一旦按下按钮,底部会被触发,函数中的b将决定从数组中选择哪个字母(即0,1 = alpha [1 - 1]),顶部是一旦有某种词汇输入。当我尝试使用上面的变量来检查所选字母是否在输入字符串中时,我遇到了问题。比如当我做console.log(存储,输入)时。它会出现身份不明,我只是需要它,所以我可以通过字符串运行字母,然后检查字母是否在单词中。我是初学者,所以我希望它不是一件麻烦事,谢谢



<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="hang.js"></script>
        <link rel="stylesheet" href="hang.css" type="text/css" />
    </head>
        <body>
            <div class="push">
                <input type="string" placeholder="What is your word ?" id="input">
                <input type="button" onclick="word(1,0)" value=Submit>
            </div>
                <h2 class="output"></h2>
                    <div class="alpha">
                        <h1 onclick="word(0,1)">A</h1>
                        <h1 onclick="word(0,2)">B</h1>
                        <h1 onclick="word(0,3)">C</h1>
                        <h1 onclick="word(0,4)">D</h1>
                        <h1 onclick="word(0,5)">E</h1>
                        <h1 onclick="word(0,6)">F</h1>
                        <h1 onclick="word(0,7)">G</h1>
                        <h1 onclick="word(0,8)">H</h1>
                        <h1 onclick="word(0,9)">I</h1>
                        <h1 onclick="word(0,10)">J</h1>
                        <h1 onclick="word(0,11)">K</h1>
                        <h1 onclick="word(0,12)">L</h1>
                        <h1 onclick="word(0,13)">M</h1>
                        <h1 onclick="word(0,14)">N</h1>
                        <h1 onclick="word(0,15)">O</h1>
                        <h1 onclick="word(0,16)">P</h1>
                        <h1 onclick="word(0,17)">Q</h1>
                        <h1 onclick="word(0,18)">R</h1>
                        <h1 onclick="word(0,19)">S</h1>
                        <h1 onclick="word(0,20)">T</h1>
                        <h1 onclick="word(0,21)">U</h1>
                        <h1 onclick="word(0,22)">V</h1>
                        <h1 onclick="word(0,23)">W</h1>
                        <h1 onclick="word(0,24)">X</h1>
                        <h1 onclick="word(0,25)">Y</h1>
                        <h1 onclick="word(0,26)">Z</h1>
                        <h1 onclick="word(0,27)">_</h1>
                        <h1 onclick="word(0,28)">-</h1>
                    </div>
        </body>     
</html>
&#13;
&#13;
&#13;

&#13;
&#13;
var alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ', '-'];
// -------------------- changes and counts word into spaces

function word(a, b) {
    if (a === 1) {
        var input = document.getElementById('input').value;
        input = input.toLowerCase();
        var store = [];

        for (var i = 0; i < input.length; i++) {
            store.push('_');
        }
        document.getElementsByClassName('output')[0].innerHTML = store;
        document.getElementById('input').value = '';
    } // -------------------------- checking if letter chosen is correct 
    else if (a === 0) {
        console.log(store, input);
    }
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

也许变量&#34;存储&#34;全局,所以下次调用word()时它不会是空的。同时将输入的初始化移到&#34;如果&#34;块:

var alpha = ['a', 'b', ...];
var store = [];

function word(){
  var input = document.getElementById('input').value; 
  if (a === 1) {
   ...
  }
}

答案 1 :(得分:0)

如果你遇到某个地方,我会为你写完整的脚本:

var secret="";
var letters=[];
var known=[];
function setsecret(a){
secret=a;
letters=secret.split("");
for(i=0;i<letters.length;i++){
known.push("_");
}
//display known
}
function check(letter){
for(i=0;i<letters.length;i++){
if(letters[i]==letter){
known[i]=letters[i];
}
}
if(letters==known){
alert("youve got it");
}
//display known
}

秘密存储我们的单词,将单词写为数组,知道猜测的单词。使用方式如下:

setsecret("ABRACADABRA");
check("A");