javascript - Checking input against array value -
ok, uploaded whole thing time. "scoring" part of code not working correctly; total doesn't increased. i'm trying loop through collection of inputs named "answer". if 1 of them checked , has same value correctanswer total increased 1.
html
<form id="quizform"> <p id="question"></p> <input type="radio" value="0" name="answer"><p class="givenchoices"></p><br> <input type="radio" value="1" name="answer"><p class="givenchoices"></p><br> <input type="radio" value="2" name="answer"><p class="givenchoices"></p><br> <input type="radio" value="3" name="answer"><p class="givenchoices"></p><br> </form> <p id="score"></p> <input type="button" id="next_button" name="next" value="next" onclick="nextquestion()"> javascript
var allquestions = [ { question: "test question 1", choices: ["david cameron", "gordon brown", "winston churchill", "tony blair"], correctanswer:0 }, { question: "test question 2", choices: ["david cameron2", "gordon brown2", "winston churchill2", "tony blair2"], correctanswer:1 }, { question: "test question 3", choices: ["david cameron3", "gordon brown3", "winston churchill3", "tony blair3"], correctanswer:2 }, { question: "test question 4", choices: ["david cameron4", "gordon brown4", "winston churchill4", "tony blair4"], correctanswer:3 }, { question: "test question 5", choices: ["david cameron5", "gordon brown5", "winston churchill5", "tony blair5"], correctanswer:3 }, ]; var total = 0; var j = 0; //initial question , ansers var currentquestion = document.getelementbyid("question"); currentquestion.innerhtml = allquestions[0].question; for(var = 0; < allquestions[j].choices.length; i++){ var currentchoices = document.getelementsbyclassname("givenchoices"); currentchoices[i].innerhtml = allquestions[j].choices[i]; }; function nextquestion(){ //last question checker if(j >= allquestions.length -1){ document.getelementbyid("quizform").style.display = "none"; document.getelementbyid("next_button").style.display = "none"; //add score document.getelementbyid("score").innerhtml = total; }; //scoring var answerlist = document.getelementsbyname("answer"); for(var = 0; < answerlist.length; i++){ if(answerlist[i].checked){ if(answerlist[i].checked.value == allquestions[j].correctanswer){ total++; } } } //show next array item j++; $("#quizform").fadeout("slow", function(){ currentquestion.innerhtml = allquestions[j].question; for(var = 0; < allquestions[j].choices.length; i++){ currentchoices[i].innerhtml = allquestions[j].choices[i]; } $("#quizform").fadein("slow"); }); };
supposing questions generated dynamic content of allquestions , propose following sample solution:
html content:
<form id="quizform"> </form> <p id="score"></p> <input type="button" id="check_answer" value="check" /> javascript:
var allquestions = [ { question: "test question 1", choices: ["david", "gordon", "winston", "tony"], correctanswer:0 }, { question: "test question 2", choices: ["david", "gordon", "winston", "tony"], correctanswer:0 } ], i, j, question_content, question, total; // generate questions for(i=0;i<allquestions.length;i++) { question = allquestions[i]; console.log(question.choices); question_content = '<p id="question-'+i+'">'+question.question+'</p>'; for(j=0;j< question.choices.length ;j++) { question_content += '<input type="radio" value="'+j+'" name="answer-'+i+'"><span class="givenchoices">'+question.choices[j]+'</span><br>'; } question_content +='<hr>'; $("#quizform").append(question_content); } // validate answers $("#check_answer").on('click',function(){ // check if questions has been answered if($('input[name^="answer-"]:checked').length !== allquestions.length){ alert("please answer questions!"); return; } total= 0; for(i=0;i<allquestions.length;i++) { question = allquestions[i]; if($('input[name="answer-'+i+'"]').is(':checked')) { if($('input[name="answer-'+i+'"]:checked').val() == question.correctanswer){ total++; } } } $("#score").html(total); }); you can check on jsfiddle example http://jsfiddle.net/0dlk5pn9/2/
Comments
Post a Comment