Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 无法访问全局变量_Function_Variables_Global Variables - Fatal编程技术网

Function 无法访问全局变量

Function 无法访问全局变量,function,variables,global-variables,Function,Variables,Global Variables,我全局定义了变量,以便以后可以在函数中访问它。但是,我从函数中得到一个值“undefined”。为什么它的价值没有体现出来 注: -全局变量是“combineDashes”,它是一个数组。 -我想在中使用的函数是“单击” 1.我尝试将变量作为参数传递给函数。不起作用。 2.我确认变量已定义,并且在定义该变量的函数中包含正确的值。 3.我确认了另一个全局变量正在很好地进入函数 在这里,combineDashes被定义为全局变量: $(document).ready(function() {

我全局定义了变量,以便以后可以在函数中访问它。但是,我从函数中得到一个值“undefined”。为什么它的价值没有体现出来

注: -全局变量是“combineDashes”,它是一个数组。 -我想在中使用的函数是“单击” 1.我尝试将变量作为参数传递给函数。不起作用。 2.我确认变量已定义,并且在定义该变量的函数中包含正确的值。 3.我确认了另一个全局变量正在很好地进入函数

在这里,combineDashes被定义为全局变量:

$(document).ready(function() {      // upon page load

        var badGuesses;   // reset bad guess counter
        var theWord;        // defines variable globally
        var combineDashes;   // defines variable globally
        var letter;     // defines variable globally
        var alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z"];   // array of letters to choose
        $("#lettersRemaining").html(alphabet);  // gets elements of the alphabet array and displays on UI
    //  F O R  B A D  G U E S S E S
        if (indices.length === 0)   //  if bad guess 
            {
                badGuesses++;    // increment bad guess counter
                $("#status").show();
                $("#status").html("Sorry, " + letter + " is incorrect. Try again.");  // status message displays
                console.log("Total badGuesses...");
                console.log(badGuesses); 

                //  remove guessed letter from alphabet
                var alphaIndex = alphabet.indexOf(letter);      // gets index of letter in alphabet
                alphabet.splice(alphaIndex,1);  // removes the letter from the alphabet array
                console.log("alphabet index of letter guessed...");
                console.log(alphaIndex);
                $("#lettersRemaining").html(alphabet);  // refreshes letters remaining

            // display correct hangman image based on how many bad guesses
                    if (badGuesses === 1) 
                    {
                        $("#hangmanGuy").html('<img src="img/Hangman-1.png" id="hangmanImg" alt="pic of hangman 1 limb" width="144" height="216">');
                    }
                    else if (badGuesses === 2)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-2.png" id="hangmanImg" alt="pic of hangman 2 limbs" width="144" height="216">');
                    }
                    else if (badGuesses === 3)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-3.png" id="hangmanImg" alt="pic of hangman 3 limbs" width="144" height="216">');
                    }
                    else if (badGuesses === 4)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-4.png" id="hangmanImg" alt="pic of hangman 4 limbs" width="144" height="216">');
                    }
                    else if (badGuesses === 5)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-5.png" id="hangmanImg" alt="pic of hangman 5 limbs" width="144" height="216">');
                    }
                    else if (badGuesses === 6)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-6.png" id="hangmanImg" alt="pic of hangman 6 limbs" width="144" height="216">');
                            $("#status").html("Game Over. Sorry, you lose. Click Start New Game and try again.");  // status message displays
                    }
            }
        //  F O R  G O O D  G U E S S E S
        else
            {
                //  remove guessed letter from alphabet
                var alphaIndex = alphabet.indexOf(letter);      // gets index of letter in alphabet

                alphabet.splice(alphaIndex,1);  // removes the letter from the alphabet array
                    console.log("alphabet index of letter guessed...");
                    console.log(alphaIndex);

                $("#lettersRemaining").html(alphabet);  // refreshes letters remaining

            // replace dash(es) with letter

                combineDashes[indices] = letter;       // <---  HUNG UP HERE !!!!   
                console.log(letter);
                console.log(combineDashes);


                $("#status").show();
                $("#status").html(letter + " is correct! Go again.");  // status message displays


            }


    });

}); 
下面是定义combineDashes(作为数组)的函数:

//N E W G A M B U T to N C L C K D
$(“#新游戏”)。当用户单击开始新游戏按钮时,单击(函数(){//。。。
$(“#状态”).hide();//游戏重置时隐藏状态部分,说明游戏结束
var字母表=[“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”、“K”、“L”、“M”、“N”、“O”、“P”、“Q”、“R”、“S”、“T”、“U”、“V”、“W”、“X”、“Z”];//重置要选择的字母数组
$(“#hangmanGuy”).html(“”)//重置刽子手图像
错误猜测=0;//重置稍后使用的猜测计数器
var wordCollection=[“大厦”、“雕像”、“大猩猩”、“笔记本”、“智能手机”、“插图”、“照片”、“优雅”、“植树人”、“键盘”、“日历”、“首都”、“教科书”、“恐怖”、“图书馆”];//计算机将随机选择的单词数组
theWord=wordCollection[Math.floor(Math.random()*wordCollection.length)];//随机选择一个单词
console.log(“这个词是……”);
console.log(theWord);
var theWordLength=theWord.length;//获取随机选择的单词中的字符数,以了解要显示的破折号数
log(“字长为…”);
log(字长);
//D I S P L A Y D A S H E S
var combineDashes=[];//创建一个数组来保存for循环中的破折号数
对于(var i=字长;i>0;i--)
{
combineDashes.push(“-”)//每个循环都会向数组中添加一个破折号
}
combineDashes.join(“”;//连接累积破折号并转换为字符串
$(“#破折号”).html(组合破折号);//在UI上显示破折号
log(“combineDashes是…”);
console.log(combineDashes);
});
下面是我尝试访问combineDashes变量的地方:

$(document).ready(function() {      // upon page load

        var badGuesses;   // reset bad guess counter
        var theWord;        // defines variable globally
        var combineDashes;   // defines variable globally
        var letter;     // defines variable globally
        var alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z"];   // array of letters to choose
        $("#lettersRemaining").html(alphabet);  // gets elements of the alphabet array and displays on UI
    //  F O R  B A D  G U E S S E S
        if (indices.length === 0)   //  if bad guess 
            {
                badGuesses++;    // increment bad guess counter
                $("#status").show();
                $("#status").html("Sorry, " + letter + " is incorrect. Try again.");  // status message displays
                console.log("Total badGuesses...");
                console.log(badGuesses); 

                //  remove guessed letter from alphabet
                var alphaIndex = alphabet.indexOf(letter);      // gets index of letter in alphabet
                alphabet.splice(alphaIndex,1);  // removes the letter from the alphabet array
                console.log("alphabet index of letter guessed...");
                console.log(alphaIndex);
                $("#lettersRemaining").html(alphabet);  // refreshes letters remaining

            // display correct hangman image based on how many bad guesses
                    if (badGuesses === 1) 
                    {
                        $("#hangmanGuy").html('<img src="img/Hangman-1.png" id="hangmanImg" alt="pic of hangman 1 limb" width="144" height="216">');
                    }
                    else if (badGuesses === 2)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-2.png" id="hangmanImg" alt="pic of hangman 2 limbs" width="144" height="216">');
                    }
                    else if (badGuesses === 3)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-3.png" id="hangmanImg" alt="pic of hangman 3 limbs" width="144" height="216">');
                    }
                    else if (badGuesses === 4)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-4.png" id="hangmanImg" alt="pic of hangman 4 limbs" width="144" height="216">');
                    }
                    else if (badGuesses === 5)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-5.png" id="hangmanImg" alt="pic of hangman 5 limbs" width="144" height="216">');
                    }
                    else if (badGuesses === 6)
                    {
                            $("#hangmanGuy").html('<img src="img/Hangman-6.png" id="hangmanImg" alt="pic of hangman 6 limbs" width="144" height="216">');
                            $("#status").html("Game Over. Sorry, you lose. Click Start New Game and try again.");  // status message displays
                    }
            }
        //  F O R  G O O D  G U E S S E S
        else
            {
                //  remove guessed letter from alphabet
                var alphaIndex = alphabet.indexOf(letter);      // gets index of letter in alphabet

                alphabet.splice(alphaIndex,1);  // removes the letter from the alphabet array
                    console.log("alphabet index of letter guessed...");
                    console.log(alphaIndex);

                $("#lettersRemaining").html(alphabet);  // refreshes letters remaining

            // replace dash(es) with letter

                combineDashes[indices] = letter;       // <---  HUNG UP HERE !!!!   
                console.log(letter);
                console.log(combineDashes);


                $("#status").show();
                $("#status").html(letter + " is correct! Go again.");  // status message displays


            }


    });

}); 
//F or B A D G U E S
if(index.length==0)//if错误猜测
{
错误猜测+++;//递增错误猜测计数器
$(“#状态”).show();
$(“#status”).html(“对不起,“+letter+”不正确。请重试。”);//显示状态消息
console.log(“总的错误猜测…”);
控制台日志(错误猜测);
//从字母表中删除猜测的字母
var alphaIndex=alphabet.indexOf(字母);//获取字母表中字母的索引
splice(alphaIndex,1);//从字母表数组中删除字母
日志(“猜测字母的字母索引…”);
控制台日志(alphaIndex);
$(“#lettersRemaining”).html(字母表);//刷新剩余的字母
//根据错误猜测的数量显示正确的刽子手图像
如果(错误猜测===1)
{
$(“#hangmanGuy”).html(“”);
}
否则如果(错误猜测===2)
{
$(“#hangmanGuy”).html(“”);
}
否则如果(错误猜测===3)
{
$(“#hangmanGuy”).html(“”);
}
否则如果(错误猜测===4)
{
$(“#hangmanGuy”).html(“”);
}
否则如果(错误猜测===5)
{
$(“#hangmanGuy”).html(“”);
}
否则如果(错误猜测===6)
{
$(“#hangmanGuy”).html(“”);
$(“#状态”).html(“游戏结束。抱歉,您输了。请单击开始新游戏,然后重试。”);//显示状态消息
}
}
//F O R G O D G U E S S
其他的
{
//从字母表中删除猜测的字母
var alphaIndex=alphabet.indexOf(字母);//获取字母表中字母的索引
splice(alphaIndex,1);//从字母表数组中删除字母
日志(“猜测字母的字母索引…”);
控制台日志(alphaIndex);
$(“#lettersRemaining”).html(字母表);//刷新剩余的字母
//用字母替换破折号

combineDashes[index]=letter;//我不是很确定,但我认为您应该从下面的行中删除“var”

var combineDashes = []; // creates an array to hold the number of dashes inside the for loop

我猜脚本创建了一个新的局部变量,该变量的名称在函数结束后被丢弃

我不是很确定,但我认为您应该从下面的行中删除“var”

var combineDashes = []; // creates an array to hold the number of dashes inside the for loop

我猜脚本创建了一个新的局部变量,该变量的名称在函数结束后被丢弃。

var combineDashes=[]应该是combineDashes=[],否则您将在想要设置全局变量的位置设置一个新的局部变量。

var combineDashes=[]应该是combineDashes=[]或者您正在设置一个新的局部变量,您想在其中设置全局变量。

@dropye我在键入时没有看到您的答案(17秒的差异),但我同意。@dropye我没有看到y