输出True或False以外的内容

时间:2012-12-16 17:02:02

标签: javascript javascript-events

Newb to JS。试图阻止如何输出问题1以外的东西是真是假。如果我理解正确,输出是标志True或False的表达式。试图改变说正确和不正确。同时尝试表达正确的百分比而不是例如:您的总分为10/100

$(function(){
    var jQuiz = {
        answers: { q1: 'd', q2: 'd', },
        questionLenght: 2,
        checkAnswers: function() {
            var arr = this.answers;
            var ans = this.userAnswers;
            var resultArr = []
            for (var p in ans) {
                var x = parseInt(p) + 1;
                var key = 'q' + x;
                var flag = false;
                if (ans[p] == 'q' + x + '-' + arr[key]) {
                    flag = true; g
                }
                else {
                    flag = false;
                }
                resultArr.push(flag);
            }
            return resultArr;
        },
        init: function(){
            $("[class=btnNext]").click(function(){
                if ($('input[type=radio]:checked:visible').length == 0) {

                    return incorrect ;
                }
                $(this).parents('.questionContainer').fadeOut(500, function(){
                    $(this).next().fadeIn(500);
                });
                var el = $('#progress');
                el.width(el.width() + 11 + 'px');
            });
            $('.btnPrev').click(function(){
                $(this).parents('.questionContainer').fadeOut(500, function(){
                    $(this).prev().fadeIn(500)
                });
                var el = $('#progress');
                el.width(el.width() - 11 + 'px');
            })
            $("[class=btnShowResult]").click(function(){
                var arr = $('input[type=radio]:checked');
                var ans = jQuiz.userAnswers = [];
                for (var i = 0, ii = arr.length; i < ii; i++) {
                    ans.push(arr[i].getAttribute('id'))
                }
            })
            $('.btnShowResult').click(function(){
                $('#progress').width(260);
                $('#progressKeeper').hide();
                var results = jQuiz.checkAnswers();
                var resultSet = '';
                var trueCount = 0;
                for (var i = 0, ii = results.length; i < ii; i++){
                    if (results[i] == true) trueCount++;
                    resultSet += '<div> Question ' + (i + 1) + ' is ' + results[i] + '</div>'
                }
                resultSet += '<div class="totalScore">Your total score is ' + trueCount * 4 + ' / 100</div>'
                $('#resultKeeper').html(resultSet).show();
            })
        }
    };
    jQuiz.init();
})

2 个答案:

答案 0 :(得分:1)

你可以改变

resultArr.push(flag);

resultArr.push(flag?"correct":"incorrect");

if (results[i] == true)

if (results[i] === "correct")

我希望百分比数是

resultSet += '<div class="totalScore">Your % correct is ' + 
  parseInt((trueCount/results.length)*100) + '%</div>'

答案 1 :(得分:0)

更改

  

'问题'+(i + 1)+'是'+ results [i] +''

  

'问题'+(i + 1)+'是'+(结果[i]?“正确”?“Incorect”)+''

其他人喜欢:

resultSet += '<div class="totalScore">Your % correct is ' + (trueCount * 4/100).toFixed(0) + '%</div>'

请通过Javascript Wiki at stackoverflow获取有关学习的提示。