mercredi 21 janvier 2015

Making a random quiz in javascript

So I'm trying to make a quiz.html file which will prompt the user for the no. of questions they want to answer. Based on their input, the random questions will be generated. So I understand it's gonna have to be a loop that will loop till the no. input into the alert box. The loop works, but the radio buttons are not unique for each question, i.e, if i select a radio button in one question, and try to select another radio button for the other question, it just comes to the present question's radio button. That is, I can only select one radio button among ALL the questions that are generated. I want to be able to select radio buttons for each quiz question. So can anyone tell me what's wrong with my code?



<div id="quesRes"></div>



<script>

var quizObj = [
{
"question": "What is the capital of Bangladesh?",
"choice": ["Dhaka", "Chittagong", "Sylhet"],
"correct": ["Dhaka"]
},
{
"question": "What does 2+2 equal to?",
"choice": ["3", "2", "4"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["4"]
},
{
"question": "What is the real name of Damon Salvatore?",
"choice": ["Paul Wesley", "Steven McQueen", "Ian Somerhalder"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["Ian Somerhalder"]
},
{
"question": "What is the name of the largest planet in the universe?",
"choice": ["Earth", "Jupiter", "Uranus"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["Jupiter"]
},
{
"question": "What is the capital of New York?",
"choice": ["Manhattan", "NYC", "Albany"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["Albany"]
},
{
"question": "How many bones does the human body have?",
"choice": ["109", "206", "114"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["206"]
},
{
"question": "What is the alter ego of Batman?",
"choice": ["Bruce Banner", "Bruce Wayne", "Tony Stark"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["Bruce Wayne"]
},
{
"question": "How many books are there in the Harry Potter series?",
"choice": ["7", "5", "8"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["7"]
},
{
"question": "What is Naruto's surname?",
"choice": ["Sarutobi", "Uchiha", "Uzumaki"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["Uzumaki"]
},
{
"question": "What is the name of Sherlock Holmes' partner?",
"choice": ["Peterson", "Watson", "Hanson"], //quizObj[2].choice[0],quizObj[2].choice[1]
"correct": ["Watson"]
},

];

var j=0;

var maxQues = prompt("How many questions do you want to answer?", "5");
var rand = Math.floor(Math.random() * maxQues);
var str='<h4>Answer all the questions</h4>';
for(var i=0;i<maxQues;i++){
j++;
var rand = Math.floor(Math.random() * maxQues);
str+=(i+1)+'. '+quizObj[rand].question+'<br>';
str+='<table>'+
'<tr><td id="a1"><input type="radio" name="radio" />'+'&nbsp;&nbsp;'+quizObj[rand].choice[0]+'</td></tr>'+
'<tr><td id="a2"><input type="radio" name="radio" />'+'&nbsp;&nbsp;'+quizObj[rand].choice[1]+'</td></tr>'+
'<tr><td id="a3"><input type="radio" name="radio"/>'+'&nbsp;&nbsp;'+quizObj[rand].choice[2]+'</td></tr>'+
'</table><br>';
}
str += '<input value="Submit" type="button" onclick="scoreMe()"><br><br>';
str += 'Score: <input id="score" type="text" size="8" placeholder="0%">';
document.getElementById('quesRes').innerHTML = str;


</script>
</div>


So i can select radio button in one question, and when i select the button for another question, it just shifts to that one





Aucun commentaire:

Enregistrer un commentaire