//global variable that takes the score and number
var totalScore = 0;
var numAnswer = 0;
//an array with objects that hold the questions
var allQuestions = [{question: ["Q1: Who is Prime Minister of the United Kingdom?"],
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:1},
{question: ["Q2: When did Columbus found America?"],
choices: ["1492", "1200", "0", "1493"],
correctAnswer:0},
{question: ["Q3: What is the Capital of Holland?"],
choices: ["The Hague", "Amsterdam", "Rotterdam", "Utrecht"],
correctAnswer:1},
{question: ["Q4: How many centimeters are there in a meter?"],
choices: ["30", "40", "50", "100"],
correctAnswer:3},
{question: ["Q5: How many universities does Amsterdam have?"],
choices: ["1", "2", "3", "6"],
correctAnswer:1}];
//create the questions created by the array
function createQuestions() {
// first unselect all radiobuttons
for (var i = 0; i<4; i++) {
document.getElementById("answer" + i).checked = false;
}
var quizQuestion = document.getElementById("questions");
quizQuestion.innerHTML = allQuestions[this.numAnswer].question[0];
createOptions();
}
//change the innerHTML of the label by calling it's ID
function createOptions() {
for (var o = 0; o <= allQuestions[0].choices[o].length; o++) {
document.getElementById("a" + o).innerHTML = allQuestions[this.numAnswer].choices[o];
}
}
//create an HTMLcollection using document.forms and check selected radiobutton and
//corresponding value.
function checkAnswers() {
var methods = document.forms.radioAnswers.elements.choice;
var answer = "error";
if (this.numAnswer<4) {
for (var i = 0; i<methods.length; i++) {
if (methods[i].checked) {
answer = methods[i].value;
break;
}
}
//check if answer is correct answer.
if...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.