在Chrome中工作的Javascript,但没有其他功能

在Chrome中工作的Javascript,但没有其他功能,javascript,firefox,google-chrome,syntax-error,Javascript,Firefox,Google Chrome,Syntax Error,我正在做这个加法问题工作表,我让它在Chrome上运行,但是下一个问题按钮在其他任何地方都不起作用。这是我的第一个Javascript程序。我通过一个验证器运行了它,并将其归结为1个错误,但注释掉这一行似乎没有帮助 下面是整个Javascript部分: <script type="text/javascript"> var questionTop = new Array(); //Array to store the top random number of questions

我正在做这个加法问题工作表,我让它在Chrome上运行,但是下一个问题按钮在其他任何地方都不起作用。这是我的第一个Javascript程序。我通过一个验证器运行了它,并将其归结为1个错误,但注释掉这一行似乎没有帮助

下面是整个Javascript部分:

 <script type="text/javascript">

var questionTop = new Array(); //Array to store the top random number of questions
var questionBot = new Array(); //Array to store the bottom random number of questions
var answers = new Array(); //Array to store the answers the user enters
var correct = new Array(); //Array to store the correct answers
var questionMarker = 0; //Loop counter for number of times through
var correctAnswers = 0; //Counter for number of correct answers

function startQuiz() {
    questionMarker = 0;  //Function starts the quiz and resets the form
    correctAnswers = 0;  //so that the first random numbers show up and hides the start button
    document.getElementById('startButton').style.visibility='hidden';
    document.getElementById('nextQuestion').style.visibility='visible';
    document.getElementById('firstAdd').innerHTML=randomNumber();
    document.getElementById('secondAdd').innerHTML=randomNumber();
}

function randomNumber()
{
    var number = Math.floor(Math.random()*50) + 1; //Generates a random number between 1-50
    return number;
}

function nextQuestion()
{
    if(txtAnswer.value === "") //Check to see if they answered the question
    {
        alert("You didn't answer the question."); //Tell them they didn't
    }
    else
    {
        questionTop[questionMarker] = document.getElementById('firstAdd').innerHTML * 1; //Stores the top random number in the array
        questionBot[questionMarker] = document.getElementById('secondAdd').innerHTML * 1; //Stores the bottom random number in the array
        answers[questionMarker] = txtAnswer.value; //Stores the answer given by the user
        correct[questionMarker] = questionTop[questionMarker] + questionBot[questionMarker]; //Calculates and stores the correct answer
        if(answers[questionMarker] == correct[questionMarker]) //Checks to see if they got the answer right
        {
            alert("You got the question right!"); //Tells them so
            correctAnswers++; //Counts the correct answer for later
        }
        else
        {
            alert("Sorry that was not the correct answer." + '\n' + "You answered " + answers[questionMarker] + '\n' + "The correct answer was " + correct[questionMarker]); //Tells them the answer if they got it wrong and compares to their answer
        }
        document.getElementById('firstAdd').innerHTML=randomNumber(); //Generates new top random number
        document.getElementById('secondAdd').innerHTML=randomNumber(); //Generates new bottom random number
        txtAnswer.value = ""; //Clears the answer field
        txtCarry.value = ""; //Clears the carry field
        questionMarker++; //Increments the questionMarker so we know how many questions we've answered.
    }
    if(questionMarker == 10) //If we've answered 10 questions...
    {
        alert("You have completed the quiz!"); //The quiz is completed
        document.write("Your Answers:"); //Displays their answers
        document.write('\n' + questionTop[0] + " + " + questionBot[0] + " = " + answers[0] + "  Correct Answer is: " + correct[0]);
        document.write('\n' + questionTop[1] + " + " + questionBot[1] + " = " + answers[1] + "  Correct Answer is: " + correct[1]);
        document.write('\n' + questionTop[2] + " + " + questionBot[2] + " = " + answers[2] + "  Correct Answer is: " + correct[2]);
        document.write('\n' + questionTop[3] + " + " + questionBot[3] + " = " + answers[3] + "  Correct Answer is: " + correct[3]);
        document.write('\n' + questionTop[4] + " + " + questionBot[4] + " = " + answers[4] + "  Correct Answer is: " + correct[4]);
        document.write('\n' + questionTop[5] + " + " + questionBot[5] + " = " + answers[5] + "  Correct Answer is: " + correct[5]);
        document.write('\n' + questionTop[6] + " + " + questionBot[6] + " = " + answers[6] + "  Correct Answer is: " + correct[6]);
        document.write('\n' + questionTop[7] + " + " + questionBot[7] + " = " + answers[7] + "  Correct Answer is: " + correct[7]);
        document.write('\n' + questionTop[8] + " + " + questionBot[8] + " = " + answers[8] + "  Correct Answer is: " + correct[8]);
        document.write('\n' + questionTop[9] + " + " + questionBot[9] + " = " + answers[9] + "  Correct Answer is: " + correct[9]);
        document.write('\n' + "You got " + correctAnswers + " answers right out of 10."); //Shows how many answers they got right
        document.write('\n' + "You got " + correctAnswers*10 + "% of the questions right."); //Calculates their percent right
        document.write('\n' + '<button id="newQuiz" type="button" onclick="window.location.reload()">New Quiz</button>'); //Creates new button to reload the screen and start again
    }
}

</script>

var questionTop=新数组()//数组以存储问题的顶部随机数
var questionBot=新数组()//数组以存储问题的底部随机数
var answers=新数组()//数组来存储用户输入的答案
var correct=新数组()//数组来存储正确的答案
var=0//循环计数器,用于通过的次数
var=0//计数器显示正确答案的数量
函数startQuiz(){
questionMarker=0;//函数启动测验并重置表单
correctAnswers=0;//因此第一个随机数显示并隐藏开始按钮
document.getElementById('startButton').style.visibility='hidden';
document.getElementById('nextQuestion').style.visibility='visible';
document.getElementById('firstAdd')。innerHTML=randomNumber();
document.getElementById('secondAdd')。innerHTML=randomNumber();
}
函数randomNumber()
{
var number=Math.floor(Math.random()*50)+1;//生成一个介于1-50之间的随机数
返回号码;
}
函数nextQuestion()
{
if(txtAnswer.value==“”)//检查他们是否回答了问题
{
警惕(“你没有回答问题。”);//告诉他们没有
}
其他的
{
questionTop[questionMarker]=document.getElementById('firstAdd')。innerHTML*1;//存储数组中的顶级随机数
questionBot[questionMarker]=document.getElementById('secondAdd')。innerHTML*1;//存储数组中底部的随机数
answers[questionMarker]=txtAnswer.value;//存储用户给出的答案
正确的[questionMarker]=questionTop[questionMarker]+questionBot[questionMarker];//计算并存储正确答案
if(answers[questionMarker]==correct[questionMarker])//检查他们的答案是否正确
{
警惕(“你答对了!”;//告诉他们
correctAnswers++;//为以后计算正确答案
}
其他的
{
警告(“对不起,这不是正确的答案。”+'\n'+“您回答了”+答案[questionMarker]+'\n'+“正确的答案是”+正确的[questionMarker]);//如果他们答错了,告诉他们答案并与他们的答案进行比较
}
document.getElementById('firstAdd')。innerHTML=randomNumber();//生成新的顶部随机数
document.getElementById('secondAdd')。innerHTML=randomNumber();//生成新的底部随机数
txtAnswer.value=“;//清除答案字段
txtCarry.value=”“;//清除进位字段
questionMarker++;//递增questionMarker,这样我们就知道我们回答了多少问题。
}
if(questionMarker==10)//如果我们已经回答了10个问题。。。
{
警报(“您已完成测验!”;//测验已完成
document.write(“您的答案:”;//显示他们的答案
document.write('\n'+questionTop[0]+“+”+questionBot[0]+“=”+answers[0]+“正确答案是:“+Correct[0]);
文档。写('\n'+questionTop[1]+“+”+questionBot[1]+“=”+answers[1]+“正确答案是:“+Correct[1]);
文档。写('\n'+questionTop[2]+“+”+questionBot[2]+“=”+answers[2]+“正确答案是:“+Correct[2]);
文档。写('\n'+questionTop[3]+“+”+questionBot[3]+“=”+answers[3]+“正确答案是:“+Correct[3]);
文档。写('\n'+questionTop[4]+“+”+questionBot[4]+“=”+answers[4]+“正确答案是:“+Correct[4]);
文档。写('\n'+questionTop[5]+“+”+questionBot[5]+“=”+answers[5]+“正确答案是:“+Correct[5]);
文档。写('\n'+questionTop[6]+“+”+questionBot[6]+“=”+answers[6]+“正确答案是:“+Correct[6]);
文档。写('\n'+questionTop[7]+“+”+questionBot[7]+“=”+answers[7]+“正确答案是:“+Correct[7]);
文档。写('\n'+questionTop[8]+“+”+questionBot[8]+“=”+answers[8]+“正确答案是:“+Correct[8]);
文档。写('\n'+questionTop[9]+“+”+questionBot[9]+“=”+answers[9]+“正确答案是:“+Correct[9]);
document.write(“\n”+“你得到了”+correctAnswers+“10个答案中的正确答案”);//显示他们得到了多少个正确答案
document.write(“\n”+“您得到了”+正确答案*10+%的正确答案。”);//计算他们的正确答案百分比
document.write('\n'+'New quick');//创建新按钮以重新加载屏幕并重新开始
}
}
它位于web上的以下位置: 代码位于iFrame中的

nextQuestion()
更改为:

function nextQuestion()
{
    var txtAnswer = document.getElementById('txtAnswer'); //<-- ADD THIS
    var txtCarry = document.getElementById('txtCarry'); //<-- AND THIS

    if(txtAnswer.value === "") //Check to see if they answered the question
    {
        alert("You didn't answer the question."); //Tell them they didn't
    }
...
}
函数nextQuestion()
{
var txtAnswer=document.getElementById('txtAnswer');//将
nextQuestion()更改为:

function nextQuestion()
{
    var txtAnswer = document.getElementById('txtAnswer'); //<-- ADD THIS
    var txtCarry = document.getElementById('txtCarry'); //<-- AND THIS

    if(txtAnswer.value === "") //Check to see if they answered the question
    {
        alert("You didn't answer the question."); //Tell them they didn't
    }
...
}
函数nextQuestion()
{

var txtAnswer=document.getElementById('txtAnswer');//您在使用前没有声明
txtAnswer
变量。

您在使用前没有声明
txtAnswer
变量。

您的问题是没有定义名为
txtAnswer
的变量。我假设Chrome将id为的DOM元素映射到具有相同名称的变量(这是一种你不能依赖的行为)。 我建议您添加这一行开始您的
nextQuestion
功能:

var txtAnswer = document.getElementById("txtAnswer");

您的问题是没有定义名为
txtAnswer
的变量