For a quiz web app, I decided the best way to store quiz questions, answers, and other user data related to the question was to use an array of objects for the quiz. I'm not sure if this is the best way to go about it since static information such as the questions and answers will be mixed with user data.
Each object in the array contains the quiz question, answer, if the user has enabled the question, and if the question has already been asked during the quiz session.
During a quiz session I wish to ask all questions that are enabled and haven't been asked yet. My script works but I am concerned it is inefficient (with a bank of 1000+ questions)? Is it alright to store user data with my quiz questions? Will this make adding additional questions down the road more difficult? Can I improve what I have or should I look for a different approach?
My script: JS Bin: http://jsbin.com/owehix/1/edit
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
window.onload = function() {
/////////////////////////////////////////////////
// CREATE SAMPLE QUIZ ARRAY TO USE IN EXAMPLE //
/////////////////////////////////////////////////
// Quiz constructor
function quizConstructor(question, answer, enabled, asked) {
this.question = question;
this.answer = answer;
this.enabled = enabled;
this.asked = asked;
}
// Create quiz array
var quiz = new Array();
// All quiz questions and answers
quiz[0] = new quizConstructor("Montgomery", "Alabama", false, 0);
quiz[1] = new quizConstructor("Juneau", "Alaska", true, 0);
quiz[2] = new quizConstructor("Phoenix", "Arizona", true, 0);
quiz[3] = new quizConstructor("Little Rock", "Arkansas", false, 0);
quiz[4] = new quizConstructor("Sacramento", "California", true, 0);
quiz[5] = new quizConstructor("Denver", "Colorado", false, 0);
quiz[6] = new quizConstructor("Hartford", "Connecticut", false, 0);
quiz[7] = new quizConstructor("Dover", "Delaware", false, 0);
quiz[8] = new quizConstructor("Tallahassee", "Florida", false, 0);
quiz[9] = new quizConstructor("Atlanta", "Georgia", true, 0);
/////////////////////////////////////////////////
// END: CREATE SAMPLE QUIZ ARRAY TO USE IN EXAMPLE //
/////////////////////////////////////////////////
// Find the number of questions that the user has enabled
var numEnabled = 0;
for (var i = 0; i < quiz.length; i++) {
if (quiz[i].enabled == true) {
numEnabled++;
}
}
// Ask all enabled questions in random order
for (var i = 0; i < numEnabled; i++) {
// Find random question that hasn't been asked yet
do {
var randomNum = Math.floor(Math.random() * quiz.length);
} while (quiz[randomNum].enabled == false || quiz[randomNum].asked == 1);
// Ask question
var question = quiz[randomNum].question + " is the capital of which state?";
document.getElementById("divSolution").innerHTML += "<p>" + question + "</p>";
// Mark question as asked
quiz[randomNum].asked++;
}
}
</script>
</head>
<body>
<div id="divSolution"></div>
</body>
</html>