Challenge 4: Mini app
- TypeScript course
- Intermediate
- Lesson 89/89
- 6 min
Solve a task testing the full intermediate level.
Mini app challenge
This challenge tests the entire intermediate level. You will combine interfaces for data models, generic functions, access modifiers, and display functions to build a working mini application. Each part carries its own responsibility and together they form a type-safe, working application.
Practice Generic first element helper
Define a Urun interface: ad: string, fiyat: number. The ilk generic function should be <T>(arr: T[]): T | undefined and return arr[0]. The urunler array should contain two Urun objects: { ad: "kalem", fiyat: 5 } and { ad: "defter", fiyat: 10 }. Assign the result of ilk(urunler) to ilkUrun and print ilkUrun.ad.
Starter code
// Urun interface tanimla
// ilk generic fonksiyonu yaz
// urunler dizisi olustur
// ilk ile ilk urunu al
// ad yazdirSample solution
interface Urun { ad: string; fiyat: number; }
function ilk<T>(arr: T[]): T | undefined {
return arr[0];
}
const urunler: Urun[] = [
{ ad: "kalem", fiyat: 5 },
{ ad: "defter", fiyat: 10 }
];
const ilkUrun = ilk(urunler);
console.log(ilkUrun.ad);Expected output
kalem
Practice Task filtering app
Define a Gorev interface: ad: string, tamamlandi: boolean. The filtrele generic function should be <T>(arr: T[], kosul: (x: T) => boolean): T[]. Inside, declare an empty sonuc: T[] array, loop with for...of, if kosul(x) is true do sonuc.push(x), then return sonuc. The gorevler array should contain two Gorev objects: { ad: "a", tamamlandi: true } and { ad: "b", tamamlandi: false }. Assign the result of filtrele(gorevler, (g) => !g.tamamlandi) to aciklar and print aciklar.length.
Starter code
// Gorev interface tanimla
// filtrele generic fonksiyonu yaz
// gorevler dizisi olustur
// tamamlanmamislari filtrele
// aciklar.length yazdirSample solution
interface Gorev { ad: string; tamamlandi: boolean; }
function filtrele<T>(arr: T[], kosul: (x: T) => boolean): T[] {
const sonuc: T[] = [];
for (const x of arr) {
if (kosul(x)) sonuc.push(x);
}
return sonuc;
}
const gorevler: Gorev[] = [
{ ad: "a", tamamlandi: true },
{ ad: "b", tamamlandi: false }
];
const aciklar = filtrele(gorevler, (g) => !g.tamamlandi);
console.log(aciklar.length);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 correct order of data model, generic function, and display steps in a mini app?
- Only generic, rest not needed
- Define model, write generic function, display Correct answer
- Only display, model not needed
- First display, then model, then generic
Explanation: Correct flow: first the data model is defined, then the generic function is written, and finally display is done.
2 Data model keyword
____ Gorev { ad: string; tamamlandi: boolean; }
interface Gorev { ad: string; tamamlandi: boolean; }
Explanation: An interface is used to define an object structure; type is an alternative but requires different syntax.
3 Mini app parts
| Term | Meaning |
|---|---|
interface |
Data model |
generic |
Type-safe helper |
extends |
Constraint |
console.log |
Output |