javascript:如何构建一个引用我的测验应用程序中变量的函数?

javascript:如何构建一个引用我的测验应用程序中变量的函数?,javascript,jquery,html,Javascript,Jquery,Html,我最近建立了一个小测验应用程序,它目前只有两个问题。完成所有问题后,我希望应用程序显示一个页面,上面写着“你在这里成功了”(最终我会添加更多)。但是由于某些原因,此代码的最终函数反馈不起作用。我哪里做错了 $(document).ready(function () { var questions = [ {question: "Who is Zack Morris?", choices: ['images/ACslater.jpg','images/CarltonBanks.jpeg',

我最近建立了一个小测验应用程序,它目前只有两个问题。完成所有问题后,我希望应用程序显示一个页面,上面写着“你在这里成功了”(最终我会添加更多)。但是由于某些原因,此代码的最终函数反馈不起作用。我哪里做错了

$(document).ready(function () {  

var questions = [
{question: "Who is Zack Morris?",
 choices: ['images/ACslater.jpg','images/CarltonBanks.jpeg','images/ZachMorris.jpg'],
 quesNum: 1,
 correctAns: 2},

 {question: "Who is Corey Matthews?",
 choices: ['images/CoryMatthews.jpeg','images/EdAlonzo.jpg','images/Shawnhunter.jpg'],
 quesNum: 2,
 correctAns: 1},

 ];

 var userAnswer  //THis needs to be looked into
 var counter = 0;
 var score = 0;
 var html_string = '';
 var string4end = ''

//function to loop choices in HTML, updates counter, checks answer

var update_html = function(currentQuestion) {
    // put current question into a variable for convenience.

   // put the question string between paragraph tags
   html_string = '<p>' + currentQuestion.question + '</p>';  
   // create an unordered list for the choices
   html_string += '<ul>';
   // loop through the choices array
   for (var j = 0; j < currentQuestion.choices.length; j++) {
      // put the image as a list item
      html_string += '<li><img src="' + currentQuestion.choices[j] + '"></li>';
   }
   html_string += '</ul>';
   $('.setImg').html(html_string);
}

update_html(questions[0]);


$('.setImg li').on('click', function (e) {
   userAnswer = $(this).index();
   checkAnswer();
   counter++;
   update_html(questions[counter]);
   $('#score').html(score);
   showFinalFeedback();
});

//function to identify right question

function checkAnswer () 
{
   if (userAnswer === questions[counter].correctAns)
   {
      score=+100;  
   }
}

function showFinalFeedback ()
{
   if (counter === (questions.length+1))
   {

      string4end = '<p>' + 'You made it here!!!!' + '</p>';  
      $('.setImg').html(string4end);

   }

}




});
$(文档).ready(函数(){
变量问题=[
{问题:“扎克·莫里斯是谁?”,
选项:['images/ACslater.jpg'、'images/CarltonBanks.jpeg'、'images/ZachMorris.jpg'],
奎斯努姆:1,
更正:2},
{问题:“科里·马修斯是谁?”,
选项:['images/CoryMatthews.jpeg'、'images/EdAlonzo.jpg'、'images/Shawnhunter.jpg'],
奎斯南:2,
更正:1},
];
var userAnswer//这需要仔细研究
var计数器=0;
var得分=0;
var html_string='';
var string4end=''
//函数在HTML中循环选择,更新计数器,检查答案
var update_html=函数(currentQuestion){
//为方便起见,将当前问题放入变量中。
//将问题字符串放在段落标记之间
html_string=''+currentQuestion.question+'

'; //为选项创建无序列表 html_字符串+='
    '; //循环选择数组 对于(var j=0;j'; $('.setImg').html(string4end); } } });
我同意Vector的观点,您可以从1开始初始化计数器,也可以从1开始检查计数器

if (counter < questions.length) {
    return;
}
alert('You \'ve made it till here');
if(计数器
我还以jquery插件的形式重写了它,也许是与您的工作方式的一个方便的比较

jsfiddle:

;(函数($){
功能问题(选项){
如果(选项类型===‘未定义’){
返回;
}
这个。选择=-1;
this.question=options.question;
this.options=options.options;
this.correct=options.correct;
this.toString=函数(){
var msg=''+this.question+'';
对于(var i=0;i=0){
var p=this.questions[this.lastQuestion];
如果(p.selected==-1){
警惕(“先回答问题!”);
返回false;
}
如果(p.selected==p.correct){
这是正确的;
}
$(this.element).html(“”);
}
这个.lastQuestion++;
if(this.lastQuestion;(function($) {
    function Question(options) {
        if (typeof options === 'undefined') {
            return;
        }

        this.chosen = -1;
        this.question = options.question;
        this.options = options.options;
        this.correct = options.correct;

        this.toString = function() {
            var msg = '<h3><i>' + this.question + '</i></h3>';
            for (var i = 0; i < this.options.length; i++) {
                msg += '<a id="opt' + i + '" class="answer toggleOff" onclick="$(this.parentNode).data(\'quizMaster\').toggle(' + i + ')">' + this.options[i] + '</a>';
            }
            return msg;
        };

        this.toggle = function(i) {
            var el = $('#opt' + i);
            if ($(el).hasClass('toggleOff')) {
                $(el).removeClass('toggleOff');
                $(el).addClass('toggleOn');
            } else {
                $(el).removeClass('toggleOn');
                $(el).addClass('toggleOff');
            }
        };
    }

    function Quiz(elem, options) {
        this.element = $(elem);
        this.lastQuestion = -1;
        this.questions = [];
        this.correct = 0;

        if (typeof options !== 'undefined' && typeof options.questions !== undefined) {
            for (var i = 0; i < options.questions.length; i++) {
                this.questions.push(new Question(options.questions[i]));
            }
        }

        this.start = function() {
            this.lastQuestion = -1;
            this.element.html('');
            for (var i = 0; i < this.questions.length; i++) {
                this.questions[i].chosen = -1;
            }
            this.correct = 0;
            this.next();
        };

        this.next = function() {
            if (this.lastQuestion >= 0) {
                var p = this.questions[this.lastQuestion];
                if (p.chosen === -1) {
                    alert('Answer the question first!');
                    return false;
                }
                if (p.chosen === p.correct) {
                    this.correct++;
                }
                $(this.element).html('');
            }
            this.lastQuestion++;
            if (this.lastQuestion < this.questions.length) {
                var q = this.questions[this.lastQuestion];
                $(this.element).html(q.toString());
                console.log(q.toString());
            } else {
                alert('you replied correct on ' + this.correct + ' out of ' + this.questions.length + ' questions');
                this.start();
            }
        };

        this.toggle = function(i) {
            if (this.lastQuestion < this.questions.length) {
                var q = this.questions[this.lastQuestion];
                q.toggle(q.chosen);
                q.toggle(i);
                q.chosen = i;
            }
        };
    }

    $.fn.quizMaster = function(options) {
        if (!this.length || typeof this.selector === 'undefined') {
            return;
        }
        var quiz = new Quiz($(this), options);
        quiz.start();
        $(this).data('quizMaster', quiz);
        $('#btnConfirmAnswer').on('click', function(e) {
            e.preventDefault();
            quiz.next();
        });
    };
}(jQuery));

$(function() {
    $('#millionaire').quizMaster({
        questions: [
            { 
                question: 'Where are the everglades?',
                options: ['Brazil','France','USA','South Africa'],
                correct: 2
            },
            { 
                question: 'Witch sport uses the term "Homerun"?',
                options: ['Basketball','Baseball','Hockey','American Football'],
                correct: 1
            }
        ]
    });
});
$('.setImg').on('click', 'li', function () {  
userAnswer = $(this).index();
checkAnswer();
counter++;
$('#score').html(score);

if (counter < questions.length)
     {
       update_html(questions[counter]);

      }
else{
         showFinalFeedback();
      }    
});