CodeBlanks Logo Icon CodeBlanks

Integration

Build the working quiz by combining all parts.

Summary - Quiz app

In this project you combined all parts of a quiz app: the question model, question pool, answer checking, and score calculation. Each part carries its own responsibility and together they form a working application. The model represents data, the pool the source, the check the logic, and the score the result.

interface Question { text: string; correctIndex: number; }
function checkAnswer(d: number, c: number): boolean {
  return d === c;
}

Final assembly

In the final assembly, all parts merge in a single flow: the question pool is taken, answer checking is done for each question, results are collected in a boolean[] array, and the total is found with skorHesapla. The map method turns each question into a boolean result.

const results = questions.map(
  (q, i) => checkAnswer(q.correctIndex, answers[i])
);

Practice Working quiz

Define a Soru interface: metin: string, dogruIndex: number. The cevapKontrol function should be (dogruIndex: number, cevap: number): boolean and return dogruIndex === cevap. The skorHesapla function should be (sonuclar: boolean[]): number, counting true values with for...of. The sorular array should contain two Soru objects: { metin: "a", dogruIndex: 0 } and { metin: "b", dogruIndex: 1 }. The cevaplar array should be [0, 0]. Use sorular.map to call cevapKontrol for each question, build the sonuclar array, and print skorHesapla(sonuclar) with console.log.

Starter code

index.ts
// Soru interface tanımla
// cevapKontrol ve skorHesapla fonksiyonları yaz
// sorular ve cevaplar dizileri oluştur
// map ile sonuçları topla, skoru yazdır

Sample solution

index.ts
interface Soru {
  metin: string;
  dogruIndex: number;
}
function cevapKontrol(dogruIndex: number, cevap: number): boolean {
  return dogruIndex === cevap;
}
function skorHesapla(sonuclar: boolean[]): number {
  let skor = 0;
  for (const s of sonuclar) {
    if (s) skor++;
  }
  return skor;
}
const sorular: Soru[] = [
  { metin: "a", dogruIndex: 0 },
  { metin: "b", dogruIndex: 1 }
];
const cevaplar: number[] = [0, 0];
const sonuclar = sorular.map((s, i) => cevapKontrol(s.dogruIndex, cevaplar[i]));
console.log(skorHesapla(sonuclar));

Expected output

1

Questions and answers

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

1 What is the integration order in a quiz app?

  • Score, check, pool, model
  • Model, check, pool, score Correct answer
  • Pool, model, score, check
  • Check, score, model, pool

Explanation: Correct order: first the model is defined, then answer checking is written, the question pool is added, and finally the score is calculated.

2 Index access with map

Question
const sonuclar = sorular.map((s, i) => cevapKontrol(s.dogruIndex, cevaplar[____]));
Answer
const sonuclar = sorular.map((s, i) => cevapKontrol(s.dogruIndex, cevaplar[i]));

Explanation: In the map callback, the i parameter gives the current index; it is used to fetch the answer at that index from the answers array.

3 Integration parts

Term Meaning
Soru interface Data model
sorular dizisi Question pool
cevapKontrol Validation function
skorHesapla Score calculation

All lessons in Intermediate