Javascript数组在重新加载时重置

时间:2014-02-10 21:54:55

标签: javascript html arrays

我是Javascript的新手并遇到了问题。我有一个Javascript文件,我已经链接到一个HTML文件,它只用了一个功能它工作正常,但现在我已经添加了第二个非常相似的功能,它已经停止工作,我已经拖了几次,不能确定它为什么会停止工作。我的HTML文件如下          

<head>
<title>Programming Aptitude Test-Question One</title>
<script type="text/javascript" src="AnswerStorage.js"></script>
</head>

<body>
<h1>Question One</h1>

<ul>
    <li>a=10</li>
    <li>b=20</li>
    <li>a=b</li>
    <li>b=a</li>
    <li>What is the value of a?</li>
    <li>What is the value of b?</li>
</ul>

<p>Please enter your answer in the form "a=?, b=?", press submit only once when you are happy with your answer and click "next question" to go to the next page</p>

<p>Enter your answer:
    <input type="text" id="firstAnswer" />
</p>

<p>
    <input type="button" id="btnSubmit" value="Submit" />
</p>

<h1>Question Two</h1>

<p>What is a word to describe "walking cautiously"?</p>

<p>Enter your answer:
    <input type="text" id="redundantAnswer" />
</p>

<p>
    <input type="button" id="btnRedundant" value="Submit" />
</p>

<h1>Question Three</h1>

<p>Writ down as many words as you can associate with the word "Career."</p>

<p>Enter your answer:
    <input type="text" id="redundantAnswer" />
</p>

<p>
    <input type="button" id="btnRedundant" value="Submit" />
</p>

<h1>Question Four</h1>

<ul>
    <li>a=5</li>
    <li>b=6</li>
    <li>a=b</li>
    <li>b=a</li>
    <li>What is the value of a?</li>
    <li>What is the value of b?</li>
</ul>

<p>Please enter your answer in the form "a=?, b=?", press submit only once when you are happy with your answer and click "next question" to go to the next page</p>

<p>Enter your answer:
    <input type="text" id="secondAnswer" />
</p>

<p>
    <input type="button" id="secondButton" value="Submit" onclick=(secondCheck()) />
</p>



<a href="Question2.html">Next Question</a>

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

我的Javascript文件如下,但我的数组没有打印,因此我无法知道是否正在添加元素

var answers = [];
var result = document.getElementById("result");


function init() {
var myButton = document.getElementById("btnSubmit");
myButton.onclick = firstCheck;

}

onload = init;


function firstCheck()

{

var theFirstAnswer = document.getElementById("firstAnswer").value.toLowerCase();
var message = "";


var success = false;

if (newName == "")

{

    alert("Please, enter an answer");

    return false;

}

if (theFirstAnswer == "a=0, b=20") {
    answers.push("m1");
    answers.push("s1");
}

if (theFirstAnswer == "a=20, b=20") {
    answers.push("m2");
    answers.push("s1");
}

if (theFirstAnswer == "a=10, b=0") {
    answers.push("m3");
    answers.push("s1");
}

if (theFirstAnswer == "a=10, b=10") {
    answers.push("m4");
    answers.push("s1");
}

if (theFirstAnswer == "a=30, b=50") {
    answers.push("m5");
    answers.push("s1");
}

if (theFirstAnswer == "a=0, b=30") {
    answers.push("m6");
    answers.push("s1");
}
if (theFirstAnswer == "a=40, b=30") {
    answers.push("m7");
    answers.push("s1");
}
if (theFirstAnswer == "a=30, b=0") {
    answers.push("m8");
    answers.push("s1");
}
if (theFirstAnswer == "a=10, b=20") {
    answers.push("m9");
    answers.push("m10");
    answers.push("s2");
}
if (theFirstAnswer == "a=20, b=10") {
    answers.push("m10");
    answers.push("s1");
}
if (theFirstAnswer == "a=0, b=10") {
    answers.push("m1");
    answers.push("s2");
}
if (theFirstAnswer == "a=20, b=0") {
    answers.push("m3");
    answers.push("s2");
}
if (theFirstAnswer == "a=10, b=30") {
    answers.push("m5");
    answers.push("s2");
}
if (theFirstAnswer == "a=30, b=20") {
    answers.push("m7");
    answers.push("s2");
}


for (var i = 0; i < answers.length; i++)

{


    if (answers[i] == theFirstAnswer)

    {

        message = "Sorry, the answer " + answers[i] + " has already been tried.  Try again";

        result.innerHTML = message;

        success = false;

        return false;

    } else

    {

        message = "Great, you've successfully registered with us as " + theFirstAnswer;

        result.innerHTML = message;

        success = true;

    }

}


if (success)

{

    answers.push(theFirstAnswer);

}


result.innerHTML += "<br />" + answers.sort();

}




function secondCheck()

{



var success = false;

var theSecondAnswer = document.getElementById("secondAnswer").value.toLowerCase();
var message = "";


var success = false;

if (theSecondAnswer == "")

{

    alert("Please, enter an answer");

    return false;

}

if (theSecondAnswer == "a=0, b=6") {
    answers.push("m1");
    answers.push("s1");
}
if (theSecondAnswer == "a=6, b=6") {
    answers.push("m2");
    answers.push("s1");
    answers.push("m4");
    answers.push("s2");
}
if (theSecondAnswer == "a=5, b=0") {
    answers.push("m3");
    answers.push("s1");
}
if (theSecondAnswer == "a=5, b=5") {
    answers.push("m4");
    answers.push("s1");
    answers.push("m2");
    answers.push("s2");
}
if (theSecondAnswer == "a=11, b=17") {
    answers.push("m5");
    answers.push("s1");
}
if (theSecondAnswer == "a=0, b=11") {
    answers.push("m6");
    answers.push("s1");
    answers.push("s2");
}
if (theSecondAnswer == "a=16, b=11") {
    answers.push("m7");
    answers.push("s1");
}
if (theSecondAnswer == "a=11, b=0") {
    answers.push("m8");
    answers.push("s1");
    answers.push("s2");
}
if (theSecondAnswer == "a=5, b=6") {
    answers.push("m9");
    answers.push("m10");
    answers.push("s2");
}
if (theSecondAnswer == "a=6, b=5") {
    answers.push("m10");
    answers.push("s1");
}
if (theSecondAnswer == "a=0, b=5") {
    answers.push("m1");
    answers.push("s2");
}
if (theSecondAnswer == "a=6, b=0") {
    answers.push("m3");
    answers.push("s2");
}
if (theSecondAnswer == "a=5, b=11") {
    answers.push("m5");
    answers.push("s2");
}
if (theSecondAnswer == "a=11, b=6") {
    answers.push("m7");
    answers.push("s2");
}
if (theSecondAnswer == "a=0, b=9, c=0") {
    answers.push("m1");
    answers.push("s1");
}
if (theSecondAnswer == "a=9, b=9, c=9") {
    answers.push("m2");
    answers.push("s1");
}
if (theSecondAnswer == "a=10, b=0, c=0") {
    answers.push("m3");
    answers.push("s1");
}
if (theSecondAnswer == "a=10, b=7, c=10") {
    answers.push("m4");
    answers.push("s1");
}
if (theSecondAnswer == "a=16, b=25, c=26") {
    answers.push("m5");
    answers.push("s1");
}
if (theSecondAnswer == "a=0, b=16, c=0") {
    answers.push("m6");
    answers.push("s1");
}
if (theSecondAnswer == "a=33, b=16, c=10") {
    answers.push("m7");
    answers.push("s1");
}
if (theSecondAnswer == "a=26, b=0, c=0") {
    answers.push("m8");
    answers.push("s1");
}
if (theSecondAnswer == "a=7, b=9, c=10") {
    answers.push("m9");
}
if (theSecondAnswer == "a=10, b=9, c=7") {
    answers.push("m10");
    answers.push("s1");
    answers.push("s2");
}
if (theSecondAnswer == "a=0, b=9, c=7") {
    answers.push("m1");
    answers.push("s2");
}
if (theSecondAnswer == "a=7, b=9, c=7") {
    answers.push("m2");
    answers.push("s2");
}
if (theSecondAnswer == "a=10, b=9, c=0") {
    answers.push("m3");
    answers.push("s2");
}
if (theSecondAnswer == "a=10, b=9, c=10") {
    answers.push("m4");
    answers.push("s2");
}
if (theSecondAnswer == "a=7, b=9, c=17") {
    answers.push("m5");
    answers.push("s2");

    if (theSecondAnswer == "a=0, b=9, c=17") {
        answers.push("m6");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=17, b=9, c=10") {
        answers.push("m7");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=17, b=9, c=0") {
        answers.push("m8");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=9, b=7, c=0") {
        answers.push("m1");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=9, b=7, c=9") {
        answers.push("m2");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=0, b=0, c=25") {
        answers.push("m3");
        answers.push("s1");
    }
    if (theSecondAnswer == "a=25, b=25, c=25") {
        answers.push("m4");
        answers.push("s1");
    }
    if (theSecondAnswer == "a=41, b=39, c=9") {
        answers.push("m5");
        answers.push("s1");
    }
    if (theSecondAnswer == "a=9, b=32, c=0") {
        answers.push("m6");
        answers.push("s1");
    }
    if (theSecondAnswer == "a=57, b=32, c=66") {
        answers.push("m7");
        answers.push("s1");
    }
    if (theSecondAnswer == "a=0, b=0, c=41") {
        answers.push("m8");
        answers.push("s1");
    }
    if (theSecondAnswer == "a=25, b=7, c=9") {
        answers.push("m9");
    }
    if (theSecondAnswer == "a=9, b=7, c=25") {
        answers.push("m10");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=0, b=7, c=25") {
        answers.push("m3");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=25, b=7, c=25") {
        answers.push("m4");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=34, b=7, c=9") {
        answers.push("m5");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=34, b=7, c=0") {
        answers.push("m6");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=25, b=7, c=34") {
        answers.push("m7");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=0, b=7, c=34") {
        answers.push("m8");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=-15") {
        answers.push("m1");
        answers.push("m2");
        answers.push("m10");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=0") {
        answers.push("m3");
        answers.push("m8");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=-6") {
        answers.push("m4");
        answers.push("m7");
        answers.push("m9");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "a=-21") {
        answers.push("m5");
        answers.push("m6");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "b=0") {
        answers.push("m1");
        answers.push("m6");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "b=-4") {
        answers.push("m2");
        answers.push("m5");
        answers.push("m9");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "b=-23") {
        answers.push("m3");
        answers.push("m4");
        answers.push("m10");
        answers.push("s1");
        answers.push("s2");
    }
    if (theSecondAnswer == "b=-27") {
        answers.push("m7");
        answers.push("m8");
        answers.push("s1");
        answers.push("s2");
    }



    for (var i = 0; i < answers.length; i++)

    {

        if (answers[i] == theSecondAnswer)

        {

            message = "Sorry, the answer " + answers[i] + " has already been attempted.  Try again";

            result.innerHTML = message;


            success = false;


            return false;

        } else

        {

            message = "Great, you've successfully registered with us as " + newName;

            result.innerHTML = message;


            success = true;

        }

    }



    if (success)

    {

        answers.push(theSecondAnswer);

    }


    result.innerHTML += "<br />" + answers.sort();

}

对不起,这可能是一个明显错误的家伙,我感谢您抽出时间来看这个。

1 个答案:

答案 0 :(得分:3)

我认为}缺失

if (theSecondAnswer == "a=7, b=9, c=17") {
    answers.push("m5");
    answers.push("s2");
} // <====  This is missing

您可以使用jsbeautifier.org等在线工具格式化文档,还可以帮助识别错误