I have created an online quiz using javascript but i have been able to randomise the questions, I am trying to eliminate duplicate question to be displayed, to do this i am storing the random value in an array and in "function check" the value is compared to see if it is already in the the array, but it is not working, how can i fix it?? please note that the random value will be stored in variable pos, which is being used to fetch the questions.
<!Doctype html>
<html>
<head>
<style>
</style>
<script type="text/javascript">
var quest=0,test,test_status,question,choice,choices,chA,chB,chC,chD,correct=0;
var pos=Math.floor(Math.random()*20)+0;
var array=[];
array.push(pos);
var questions=[
["What is 1+1","3","2","5","11","B"],
["What is 10+10","12","13","20","100","C"],
["What is 3+2","5","32","56","28","A"],
["what is 1-1","1","20","4","0","D"],
["what is 0-1","1","20","4","-1","D"],
["who is loky","thor's brother","the hulk","thanos","none","A"],
["what is a whale","mammal","fish","bird","insect","A"],
["what is a shark","mammal","fish","bird","insect","A"],
["what is a pc","personal computer","pass check","program counter","prime combo","A"],
["what is the 3mod2","1","2","3","none","A"],
["what is the color of the sky","red","blue","white","green","B"],
["who was the first man on the moon","neil armstrong","bruce willis","john travolta","rocco siffredi","A"],
["what is the name of this website","Dino era","Dino bots","Dino hunter","Dino crisis","A"],
["what is the name of the developper of dino era","Brandon","harold","Kersley","Jane","C"],
["who is optimus prime","president of us","leader of autobots","a common man","your dad","B"],
["what are the power rangers","bunch of faggots","teletubbies","superheroes","don't know","C"],
["who is goku","dbz character","teletubbies","soul eater character","death note character","A"],
["how many star wars movies are there","2","3","7","6","D"],
["what is the weapon that rebels destroyed in starwars, a new hope","death cannon","death star","death creator","lord of death","B"],
["what is the opposite of a jedi","storm troopers","emperor","sith","ti-fighter","C"]
];
function elements(x)
{
return document.getElementById(x);
}
function render()
{
test= elements("test");
if(quest>=10 )
{
test.innerHTML= "<h2>You Got "+correct+" of "+quest+" questions correct</h2>";
elements("status").innerHTML ="Test Completed";
return(false);
}
elements("status").innerHTML= "Question " +(quest+1);
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
chD = questions[pos][4];
test.innerHTML="<h3>"+question+"</h3>";
test.innerHTML+="<input type='radio' id='choices1' name='choice' value='A'> "+chA+"<br />";
test.innerHTML+="<input type='radio' id='choices2' name='choice' value='B'> "+chB+"<br />";
test.innerHTML+="<input type='radio' id='choices3' name='choice' value='C'> "+chC+"<br />";
test.innerHTML+="<input type='radio' id='choices4' name='choice' value='D'> "+chD+"<br /><br />";
test.innerHTML+="<button onclick='check()'>Submit</button>";
}
function check()
{
choices=document.getElementsByName("choice");
for(var i=0; i<choices.length; i++)
{
if(choices[i].checked)
{
choice=choices[i].value;
break;
}
}
if(i==choices.length)
{
alert('Select an answer');
return(false);
}
if(choice == questions[pos][5])
{
correct++;
}
pos=Math.floor(Math.random()*question.length)+0;
for(i=0; i<array.length; i++)
{
if(array[i]!=pos)
{
pos=Math.floor(Math.random()*question.length)+0;
}
}
array.push(pos);
quest++;
render();
}
window.addEventListener("load",render,false);
</script>
</head>
<body>
<h2 id="status"></h2>
<div id="test"></div>
</body>
</html>