2

Why does the click method return undefined after the for in loop iterates over the first array of answer objects?

var i = 0;
var j = 0;

var allQuestions = [{question:"What is the color of the sky?",answer:["blue","yellow",  
                "red"],correctAnswer:["blue",1]},{question:"What is the opposite of  
                 up?",answer:["down","sideways", "circle"],correctAnswer:["down",1]},
                {question:"What is the first number?",answer:["1","5", "7"],correctAnswer:
                ["1",1]}];

$(document).ready(function() {  

    function changeQuestion() {
        $("#radios").empty();
        for( answers in allQuestions[i].answer) {
            var radioBtn = $('<input type="radio" class="radios" name="btnAnswers" value="'+ 
                allQuestions[i].answer[j] + '" /><label for ="secondbtn">'
                + allQuestions[i].answer[j] + '</label>');
            radioBtn.appendTo('#radios');
            alert(allQuestions[i].answer[j]);
            j++;
        }
        i++;
        return true;
    };

    $("#nextbtn").click(function(){
        changeQuestion();
    });
});

This question's fiddle

2
  • 1
    Where are i and j initialized? Commented Nov 6, 2015 at 0:13
  • @Roamer-1888 Sorry, didn't add them in the initial post. They are there now but still doesn't work. Commented Nov 6, 2015 at 1:27

2 Answers 2

1

As the .answer property of the objects is an array, you can loop with

for(var j=0; allQuestions[i].answer.length; j++) {...}

instead of

for(answers in allQuestions[i].answer) {...}

Full code will be :

$(document).ready(function() {
    var i = 0;
    function changeQuestion() {
        $("#radios").empty();
        var obj = allQuestions[i];
        for(var j=0; j<obj.answer.length; j++) {
            $('<input type="radio" class="radios" name="btnAnswers" value="' + obj.answer[j] + '" /><label for ="secondbtn">' + obj.answer[j] + '</label>').appendTo('#radios');
        }
        i++;
    };
    $("#nextbtn").click(changeQuestion);
});

https://jsfiddle.net/zu9e5poh/10/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this worked! Can you explain (or provide links ) why the "for in" loop returned undefined on the second run of changeQuestion?
Your for(... in ...) construction isn't actually the culprit. It's the j counter, which keeps on incrementing, so on the second run, it will start at 3. If you were to move var j = 0 inside changeQuestion(), then it will be reset to zero at the start of every run. In my version, for(var j=0; ...) has exactly the same effect.
0

you have to initialize values to variables i & j

modify the code to

    $(document).ready(function() {  
function changeQuestion() {
$("#radios").empty();

var  i=0, j=0;

for( answers in allQuestions[i].answer) {
    var radioBtn = $('<input type="radio" class="radios" name="btnAnswers" value="'+ 
                    allQuestions[i].answer[j] + '" /><label for ="secondbtn">' 
            + allQuestions[i].answer[j] + '</label>');
    radioBtn.appendTo('#radios');
    alert(allQuestions[i].answer[j]);
 j++;
}
 i++;
 return true;
};
$("#nextbtn").click(function(){
    changeQuestion();
 });

});

1 Comment

@MJD I added the initialization and array in the fiddle, as well as an alert to show how the code steps but it's not working still.

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.