I’m struggling to make a flexible data structure (like a relational database) for a quiz in ActionScript 3.0.
I have a number of questions, each with 2 alternatives:
Questions
| id | alt1 | alt2 | correctAlt | --------------------------------------- | 0 | Sweden | Denmark | 1 | | 1 | Norway | Finland | 2 | | 2 | Iceland | Ireland | 1 |
And a number of users with unique IDs:
Users
| id | age | sex | profession | totalCorrect | ------------------------------------------------- | A5 | 25 | 0 | "Lorem" | 0 | | A6 | 45 | 1 | "Ipsum" | 0 | | A7 | 32 | 1 | "Dolor" | 0 |
And each user might answer a question:
Answers
| question_id | user_id | answer | ---------------------------------- | 0 | A5 | 1 | | 1 | A6 | 2 | | 2 | A7 | 1 |
How can I represent this in AS3?
And how can I, when I’ve collected all the data, answer questions such as:
a. How many users answered question 1?
b. How many % of the users answering question 1 were correct?
c. And how can I sum the number of correct answers for each user and update the totalCorrect column?
Whether you use a database or not, it’s still possible to represent this data with objects. You can create specific classes.
Here’s a basic example for a Question class.
public class Question { private var _id:int; private var _alternatives:Array; private var _correctAnswer:int; public function Question() { } //----------- Group: Getters & Setters // follow the same principle as alternatives for id & correctAnswer public function set alternatives(value:Array):void { _alternatives = value; } public function get alternatives():Array { return _alternatives; } // etc..... } var question1:Question = new Question(); question1.alternatives = ["Sweden" , "Denmark"] question.correctAnswer = 1; question.id = 0;It’s also possible to pass parameters to the constructor
public class Question { private var _id:int; private var _alternatives:Array; private var _correctAnswer:int; public function Question(id:int , alt:Array, correct:int) { _id = id; _alternatives = alt; _correctAnswer = correct; } var question1:Question = new Question(0 , ["Sweden" , "Denmark"] , 1);Do something similar for the Users , then create another class where you’ll manipulate the data.
Try and find some resources about Classes in general and Object Oriented Programming in particular.