My code works fine. I just want to have your insight and opinions about the way I did it. For example, is it easy to maintain? Does it have good architecture and design?
<html>
<head>
<title>Quizz</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
var quiz = [{
"question": "What is function of respiratory system?",
"choices": ["help u gain nutrients", "to filter and eliminate wastes", "to bring the oxygens into the body and expel carbon dioxide"],
"answer": "to bring the oxygens into the body and expel carbon dioxide"
}, {
"question": "What body system is responsible for protections of body tissues and enabling human body movements",
"choices": ["Skeleton System", "Digestive system", "Circular system"],
"answer": "Skeleton System"
}, {
"question": "How many body systems are there in total",
"choices": ["4", "5", "6"],
"answer": "6"
}, {
"question": "What system is responsible for bringing energy to the body for cell growth and repair?",
"choices": ["Digestive", "Respiratory", "Circulatory"],
"answer": "Digestive"
}];
var currentQuestion = 0,
score = 0;
function loadquestions(){
var choices = quiz[currentQuestion].choices,
choicesHtml = "";
for (var i = 0; i < choices.length; i++) {
choicesHtml += "<input type='radio' name='quiz" + currentQuestion +
"' id='choice" + (i + 1) +
"' value='" + choices[i] + "'>" +
" <label>" + choices[i] + "</label><br>";
}
document.getElementById('questions').textContent = "Q" + (currentQuestion + 1) + ". " +quiz[currentQuestion].question;
// load the choices
document.getElementById('choices').innerHTML = choicesHtml;
}
function checkanswers(){
var userpick='';
var pick=document.getElementsByName('quiz'+currentQuestion);
alert(pick.length);
for (var i = 0; i < pick.length; i++) {
if (pick[i].checked) { // if this radio button is checked
userpick = pick[i].value;
}
}
if(userpick=='')
alert('You havenot selected an answer');
else{
if(userpick==quiz[currentQuestion].answer){
score++;
document.getElementById('answers').innerHTML='you answer the question correctly';
$('#answers').css('background-color','green');
$('#check').replaceWith( '<button id="nextanswer" onclick="nextanswer()">Next Question</button>' )
}
else{
document.getElementById('answers').innerHTML='you answer the question incorrectly. Answer:'+quiz[currentQuestion].answer;
$('#answers').css('background-color','red');
$('#check').replaceWith( '<button id="nextanswer" onclick="nextanswer()">Next Question</button>' );
}
if(currentQuestion+1==quiz.length)
{
$('#nextanswer').css('display','none');
$('#overrallresult').text('You answered correctly '+score+' out of '+ quiz.length+ ' questions');
}
}
}
function nextanswer(){
$('#nextanswer').replaceWith('<button id="check" onclick="checkanswers()">Submit</button> ');
$('#answers').text('');
currentQuestion++;
loadquestions();
}
window.addEventListener("load", loadquestions);
</script>
</head><header> <embed src="logo.swf" height="10%" width="100%" /> </header>
<body>
<div id='questions'>
</div>
<br>
<div id='choices'>
</div>
<div id='answers' >
</div>
<div id='overrallresult'>
</div>
<br>
<button id='check' onclick='checkanswers()'>Submit</button>
</body>
</html>
jQuery
, he could use the methodjQuery.getJSON()
for this. It doesn't even need to be a script, it could be thejson
file itself stored in the server. \$\endgroup\$