Skip to content

Instantly share code, notes, and snippets.

@slopeofhope81
Created February 17, 2014 19:14
Show Gist options
  • Select an option

  • Save slopeofhope81/9057028 to your computer and use it in GitHub Desktop.

Select an option

Save slopeofhope81/9057028 to your computer and use it in GitHub Desktop.
Making a quiz questions #1
var allQuestions = [];
allQuestions[0]={
question: "what is your name?",
choices:["steve", "kevin", "chloe","haeseong"],
correctAnswer: 1
};
allQuestions[1]={
question: "what is your passion?",
choices:["programmer", "teacher", "scientist","home builder"],
correctAnswer: 1
};
allQuestions[2]={
question: "what is your favorite movie?",
choices:["superman", "batman", "mickey Mouse", "none"],
correctAnswer: 1
};
var questionNumber = 0;
var score = 0;
var next=document.getElementById("next");
var userChoice = null;
function showScore(){
var quiz =document.getElementById("quiz");
quiz.style.display="none";
var scoreBoard=document.getElementById("scoreBoard");
scoreBoard.innerHTML="you score is: " + score;
}
function displayQuestion(qn){
if (allQuestions.length == questionNumber){
return showScore();
}
else{
for (var i =1; i < allQuestions[qn].choices.length;i++){
allQuestions[qn].choices.checked = false;
}
var questionLocation=document.getElementById("question");
questionLocation.innerHTML=allQuestions[qn].question;
for(var i =1; i < allQuestions[qn].choices.length; i++){
document.getElementById("choiceText"+i).innerHTML=allQuestions[qn].choices[i];
}
}
};
function checkAnswer(){
for(var i =1; i < allQuestions.choices.length; i++){
if (document.forms.quiz.elements.choice[i].checked){
if ( document.forms.quiz.elements.choice[i].value== allQuestions[questionNumber].correctAnswer){
score++;
}
}
}
questionNumber++;
displayQuestion(questionNumber);
}
next.onclick=function(){
checkAnswer();
}
window.onload=function(){
displayQuestion(questionNumber);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment