CodeBlanks Logo Icon CodeBlanks

Tamamlama

Bir görevi tamamlanmış olarak işaretlemeyi öğrenirsin.

completeTodo fonksiyonu

completeTodo, bir görevi id'ye göre bulup status'ünü "completed" yapar. Görev listeden silinmez; yalnızca durumu güncellenir. find ile görevi bulur, if ile var mı kontrol ederiz.

function completeTodo(todos: Todo[], id: string): void {
  const todo = todos.find(t => t.id === id);
  if (todo) {
    todo.status = "completed";
  }
}

Toggle ile ters çevirme

Bir görevin durumunu ters çevirmek için toggleTodo yazabilirsin. Görev "active" ise "completed" yapar, "completed" ise "active" yapar. Bu, bir görevi tekrar açmak için kullanışlıdır.

function toggleTodo(todo: Todo): void {
  if (todo.status === "active") {
    todo.status = "completed";
  } else {
    todo.status = "active";
  }
}

Uygulama completeTodo fonksiyonu yaz

Status ve Todo tanımla. addTodo ve listTodos fonksiyonlarını yaz. completeTodo(todos: Todo[], id: string): void fonksiyonu yaz: find ile görevi bul, varsa status'ünü "completed" yap. "Süt al" görevini ekle, "1" id ile tamamla ve listele.

Başlangıç kodu

index.ts
// Status ve Todo tanımla
// addTodo ve listTodos fonksiyonlarını yaz
// completeTodo(todos: Todo[], id: string): void yaz
// id'ye göre görevi bul ve status'ü "completed" yap
// bir görev ekle, tamamla ve listele

Örnek çözüm

index.ts
type Status = "active" | "completed";
interface Todo { id: string; title: string; status: Status; }

function addTodo(todos: Todo[], title: string): Todo[] {
  const todo: Todo = { id: String(todos.length + 1), title, status: "active" };
  todos.push(todo);
  return todos;
}

function completeTodo(todos: Todo[], id: string): void {
  const todo = todos.find(t => t.id === id);
  if (todo) {
    todo.status = "completed";
  }
}

function listTodos(todos: Todo[]): void {
  todos.forEach(t => console.log(t.id + ": " + t.title + " [" + t.status + "]"));
}

const todos: Todo[] = [];
addTodo(todos, "Süt al");
completeTodo(todos, "1");
listTodos(todos);

Beklenen çıktı

1: Süt al [completed]

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 Bir görevi tamamlamak için status alanı ne yapılır?

  • Silinir
  • "completed" yapılır Doğru cevap
  • title değiştirilir
  • "active" yapılır

Açıklama: Bir görevi tamamlamak, status alanını "completed" yapmaktır. Görev listeden silinmez; title da değişmez.

2 find koşulunu tamamla

Soru
const todo = todos.find(t => t.id === ____);
Cevap
const todo = todos.find(t => t.id === id);

Açıklama: Görevi id'ye göre bulursun: t.id === id. title parametrede yok, status Status türünde (string değil), t ise Todo nesnesidir.

3 Tamamlama parçaları

Kavram Anlamı
completeTodo Görevi tamamla
todos.find(...) İd'ye göre bul
todo.status = "completed" Durumu güncelle
toggleTodo Durumu ters çevir

Başlangıç bölümündeki tüm dersler