TypeScript
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])
);
Exercise 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.
Show example solution
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));
Test yourself
-
What is the integration order in a quiz app?
- Score, check, pool, model
- Model, check, pool, score
- Pool, model, score, check
- Check, score, model, pool
Show answer
Model, check, pool, score — Correct order: first the model is defined, then answer checking is written, the question pool is added, and finally the score is calculated.
-
Index access with map
const sonuclar = sorular.map((s, i) => cevapKontrol(s.dogruIndex, cevaplar[____]));
Show answer
const sonuclar = sorular.map((s, i) => cevapKontrol(s.dogruIndex, cevaplar[i]));
-
Integration parts
- Soru interface
- Data model
- sorular dizisi
- Question pool
- cevapKontrol
- Validation function
- skorHesapla
- Score calculation
Next lesson: Weather model