0

I am trying to figure out why, when I run this code, I am getting undefined for my correct answers.

$(document).ready (function () {
//  

var answers = 
[["Fee","Fi","Fo"], 
["La","Dee","Da"]],
questions = 
["Fee-ing?",
"La-ing?"],
corAns = ["Fee", "La"];

var counter = 0;

var $facts = $('#main_ .facts_div'),
$question = $facts.find('.question'),
$ul = $facts.find('ul'),
$btn = $('.myBtn');


$btn.on('click', function() {
    if (counter < questions.length) {
        $question.text(questions[counter]);

        var ansstring = $.map(answers[counter], function(value) {
            return '<li><input type="radio" name="ans" value="0"/>'
            + value + '</li>'}).join('');
        $ul.html(ansstring);

        var currentAnswers = $('input[name="ans"]:checked').map(function() {
            return this.val();
        }).get();

        var correct = 0;
        if (currentAnswers[counter]==corAns[counter]) {
            correct++;
        }

    }
    else {
        $facts.text('You are done with the quiz ' + correct);
        $(this).hide();
    }
        counter++;
});


//    
});

It is quite long and I'm sorry about that, but I don't really know how tostrip it down.

I also realize this isn't the most elegant way to do this, but I just want to know why I can't seem to get my radio values.

I will add the markup as well if anyone wants.

 <div id="main_">
    <div class="facts_div">
        <span class="question"></span>
        <ul></ul>
    </div>
    <div id = "next_button">
    <form>
        <input id="x" type="button" class="myBtn" value="Press Me">
    </form>
    </div>
</div>
</div>
3
  • 1
    Yes, please add the markup; we won't be able to fully evaluate your code otherwise. Commented Oct 29, 2013 at 3:23
  • for one of your previous questions I had added a fiddle can you edit it to replicate this case Commented Oct 29, 2013 at 3:31
  • I updated it... It works right there until the end. Commented Oct 29, 2013 at 3:40

1 Answer 1

0

Try

$(document).ready(function () {
    //  

    var answers = [
        ["Fee", "Fi", "Fo"],
        ["La", "Dee", "Da"]
    ],
        questions = ["Fee-ing?",
            "La-ing?"],
        corAns = ["Fee", "La"];

    var counter, correct = 0;

    var $facts = $('#main_ .facts_div'),
        $question = $facts.find('.question'),
        $ul = $facts.find('ul'),
        $btn = $('.myBtn');


    $btn.on('click', function () {

        if (counter >= 0) {
            var ans = $('input[name="ans"]:checked').val();
            if (!ans) {
                alert('please select an answer');
                return;
            }
            console.log(ans, counter, corAns[counter])
            if (ans == corAns[counter]) {
                ++correct;
            }

            counter++;
        } else {
            counter = 0;
        }


        if (counter < questions.length) {
            $question.text(questions[counter]);

            var ansstring = $.map(answers[counter], function (value) {
                return '<li><input type="radio" name="ans" value="' + value + '"/>' + value + '</li>'
            }).join('');
            $ul.html(ansstring);
        } else {
            $facts.text('You are done with the quiz ' + correct);
            $(this).hide();
            $('#correct').text()
        }
    });


    //    
});

Demo: Fiddle

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.