CodeBlanks Logo Icon CodeBlanks

Question pool

Write the question pool as a module.

Question pool module

The question pool is a data source containing all questions of the quiz. It is written as a module and exported with export. This way other files can use the pool via import. Keeping questions separate separates data from logic and makes maintenance easier.

// questions.ts
export const questions: Question[] = [
  { text: "a?", options: ["x", "y"], correctIndex: 0 }
];

Type-safe question array

The question pool is a Soru[] array. Thanks to the type annotation, the compiler errors if you add a misshapen question. This is the safest way to maintain data consistency. For example, a question missing dogruIndex is caught at compile time.

const questions: Question[] = [
  { text: "2+2?", options: ["3", "4"], correctIndex: 1 }
];

Practice Create a question pool

Define a Soru interface: metin: string, dogruIndex: number. Create an array named havuz of type Soru[] and add two questions: first with text "soru1" and dogruIndex: 0, second with text "soru2" and dogruIndex: 1. Print havuz.length with console.log.

Starter code

index.ts
// Soru interface tanımla
// havuz adında Soru[] dizisi oluştur
// iki soru ekle
// havuz.length yazdır

Sample solution

index.ts
interface Soru {
  metin: string;
  dogruIndex: number;
}
const havuz: Soru[] = [
  { metin: "soru1", dogruIndex: 0 },
  { metin: "soru2", dogruIndex: 1 }
];
console.log(havuz.length);

Expected output

2

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is done to make the question pool usable from other files?

  • Declared with const
  • Printed with console.log
  • Placed inside an interface
  • Exported with export Correct answer

Explanation: The export keyword is used to export a value from a module; other files access it via import.

2 Array type annotation

Question
const sorular: ____[] = [
  { metin: "a?", siklar: ["x"], dogruIndex: 0 }
];
Answer
const sorular: Soru[] = [
  { metin: "a?", siklar: ["x"], dogruIndex: 0 }
];

Explanation: An array of Soru objects is annotated as Soru[]; this provides compile-time checking.

3 Question pool concepts

Term Meaning
export Export
Soru[] Question array type
modül Data-logic separation
import Import

All lessons in Intermediate