Output something other than True or False

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2012-12-16T17:02:01Z Indexed on 2012/12/16 17:02 UTC
Read the original article Hit count: 209

Newb to JS. Trying to determain how to to output something other than Question 1 is True and False. If I understand this correctly, the output is the expression of the flag True or False. Trying to change to say Correct and Incorrect. Also trying to express a percentage of correct instead of the for example: Your total score is 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();
})

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about javascript-events