trying to get an understanding of OOP in JavaScript

R

Richard Hollenbeck

I've asked about this project before, but now I'm going to try to rewrite my
quiz program using OOP. It already works great using just plain
old-fashioned arrrays, but it is a maintenence nightmare.

If you look at the following, does this look like something I could start
with?:

function Question(q,c,a,u,n) {
this.question = q;
this.choice = c; // possible choices like "A, B, C, or D"
this.answer=a; // this would be the "correct" answer.
this.useranswer=u; // this would be the radio button the user selects
this.note=n;
}

Maybe I don't need to put the user's
answer in there at all--or do I?

the "this.note" is just in case I want to
add any comments about the question
or a comment about the correct answer
after the user answers--maybe in an "alert"
or in a "document.write."

I don't know if the question will have four
possible choices or three or five. Each
question might be different. Is that a
problem?

Also, I want to be able to randomize the
order of the questions and assign question
numbers at runtime.

Any ideas? Thanks.

Rich Hollenbeck
 
T

Thomas 'PointedEars' Lahn

Richard said:
I've asked about this project before, but now I'm going to try to rewrite my
quiz program using OOP. It already works great using just plain
old-fashioned arrrays, but it is a maintenence nightmare.

If you look at the following, does this look like something I could start
with?:

function Question(q,c,a,u,n) {
this.question = q;
this.choice = c; // possible choices like "A, B, C, or D"

Should be _choices_, no?
this.answer=a; // this would be the "correct" answer.
this.useranswer=u; // this would be the radio button the user selects
this.note=n;
}

If you don't like many arguments, consider passing an Object object
reference instead:

function Question(data)
{
this.question = data.question;
this.choices = data.choices;
this.answer = data.answer;
this.note = data.note;
}

new Question({
question: "What is the ultimate answer to life, the universe, and
everything?",
choices: {
A: "Ravenous Bugblatter Beast of Traal",
B: 42,
C: "Guildford"
},
answer: "B",
note: "(see: THHGTTG)"
});
Maybe I don't need to put the user's
answer in there at all--or do I?

Not at first. For evaluation, you can augment the object with the property
later.
the "this.note" is just in case I want to
add any comments about the question
or a comment about the correct answer
after the user answers--maybe in an "alert"
or in a "document.write."
OK.

I don't know if the question will have four
possible choices or three or five. Each
question might be different. Is that a
problem?

I don't think so.
Also, I want to be able to randomize the
order of the questions and assign question
numbers at runtime.

You can make an Array of Questions.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top