Adding questions
- Node.js course
- Intermediate
- Lesson 43/116
- 6 min
Learn to add questions with the addQuestion method.
The Quiz class and addQuestion
Now we define the Quiz class. This class stores questions in an array. The constructor creates an empty array: this.questions = [];. The addQuestion method adds a new question.
The addQuestion(text, answer) method auto-generates an id: const id = this.questions.length + 1;. Then it builds the object with new Question(id, text, answer) and appends it with this.questions.push(...). This is a builder pattern: each call adds a question and the quiz grows. We take the previous lesson's Question class and add the Quiz class. The quiz becomes a container that manages questions.
The builder pattern
addQuestion is a builder pattern. Each call adds a question to the quiz. Example: quiz.addQuestion("Capital?", "Ankara"); quiz.addQuestion("Largest city?", "Istanbul");. After two calls quiz.questions.length is 2.
The builder pattern is the basic way to construct objects step by step. Instead of giving all data in one constructor, you add each piece with a separate method. This makes the code readable: each addQuestion call defines one question. Also, since the id is auto-generated, the call order determines the id. The first call gets id=1, the second id=2. This keeps the quiz consistent.
Practice Add the Quiz class and addQuestion method
Take the previous lesson's Question class. Add class Quiz: this.questions = [] in the constructor, addQuestion(text, answer) with const id = this.questions.length + 1; this.questions.push(new Question(id, text, answer));. The last lines are ready: add two questions and print the count.
Starter code
class Question {
constructor(id, text, answer) {
this.id = id;
this.text = text;
this.answer = answer;
}
}
class Quiz {
constructor() {
this.questions = [];
}
// Add the addQuestion method
// write here
}
const quiz = new Quiz();
quiz.addQuestion("Capital?", "Ankara");
quiz.addQuestion("Largest city?", "Istanbul");
console.log("Question count: " + quiz.questions.length);Sample solution
class Question {
constructor(id, text, answer) {
this.id = id;
this.text = text;
this.answer = answer;
}
}
class Quiz {
constructor() {
this.questions = [];
}
addQuestion(text, answer) {
const id = this.questions.length + 1;
this.questions.push(new Question(id, text, answer));
}
}
const quiz = new Quiz();
quiz.addQuestion("Capital?", "Ankara");
quiz.addQuestion("Largest city?", "Istanbul");
console.log("Question count: " + quiz.questions.length);Expected output
Soru sayısı: 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the addQuestion body
addQuestion(text, answer) {
const id = this.questions.length + 1;
this.questions.____(new Question(id, text, answer));
}
addQuestion(text, answer) {
const id = this.questions.length + 1;
this.questions.push(new Question(id, text, answer));
}
Explanation: push appends the new question to the array.
2 Match each piece to its role
| Term | Meaning |
|---|---|
this.questions |
Question array |
addQuestion |
Adds a question |
questions.length + 1 |
Generates a new id |
new Question |
Creates a question object |
3 What happens when quiz.addQuestion is called?
quiz.addQuestion("Soru?", "Cevap")- The first question is updated
- Quiz silinir
- Yeni soru dizinin sonuna eklenir Correct answer
- The question count decreases
Explanation: addQuestion appends a new question to the array.