为什么我的变量在javascript中返回未定义?

问题描述 投票:-1回答:1

我是javascript新手。我正在制作抽认卡程序。由于某种原因,尽管将变量lastPress定义为全局变量,但它似乎返回undefined。但是,这不会导致JSHint错误。这是我的代码:

           var i = 0;
            var questions = ["1: Was the human migration caused by an ice age?", "True"];
            var answer = [true, true];
            var lastPress;
            document.getElementById("question").innerHTML = questions[i];
            function itsTrue() {
                lastPress = true;
            }
            function itsFalse() {
                lastPress = false;
            }
            function getAns() {
                if (answer[i] == lastPress) {
                    document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress;
                }
                else {
                    document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i];
                }
                i++;
                document.getElementById("question").innerHTML = questions[i];
            }
            document.getElementById("true").onclick = function(){itsTrue()};
            document.getElementById("true").onclick = function(){getAns()};
            document.getElementById("false").onclick = function(){itsFalse()};
            document.getElementById("false").onclick = function(){getAns()};
<!DOCTYPE HTML>
<html>
    <head>
        <title>Flash cards</title>
    </head>
    <body>
        <p id="question">This is where the question shows up</p>
        <p id="ans">This is where the result will show up</p>
        <button type="button" id="true">True</button>
        <button type="button" id="false">False</button>
    </body>
</html>
javascript global-variables
1个回答
0
投票

在您的问题中,不清楚返回未定义的内容,所以我认为这是您的HTML中更新的内容。

您的代码返回的是未定义的,因为您每次单击都会增加i。但是您的数组只有两个元素长。您需要处理所有问题均已显示的事实。在这里,我只是将变量i重置为0,但是我不确定这是否适用于您的情况。

var i = 0;
            var questions = ["1: Was the human migration caused by an ice age?", "True"];
            var answer = [true, true];
            var lastPress;
            document.getElementById("question").innerHTML = questions[i];
            function itsTrue() {
                lastPress = true;
            }
            function itsFalse() {
                lastPress = false;
            }
            function getAns() {
                if (answer[i] == lastPress) {
                    document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress;
                }
                else {
                    document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i];
                }
                i++;
                if(i >= questions.length) {
                  i = 0;
                }
                document.getElementById("question").innerHTML = questions[i];
            }
            document.getElementById("true").onclick = function(){itsTrue(); getAns() };
            document.getElementById("false").onclick = function(){itsFalse(); getAns()};
<!DOCTYPE HTML>
<html>
    <head>
        <title>Flash cards</title>
    </head>
    <body>
        <p id="question">This is where the question shows up</p>
        <p id="ans">This is where the result will show up</p>
        <button type="button" id="true">True</button>
        <button type="button" id="false">False</button>
    </body>
</html>

我认为您应该重新考虑比较逻辑和存储请求的方式。也许把它放在一个对象数组中?因为让您的答案成为数组中问题的每个后继元素只是一种不好的做法。

© www.soinside.com 2019 - 2024. All rights reserved.