Menu
CoddyTech

Menangani Situasi saat list Kosong

Termasuk bagian Dasar-Dasar dalam perjalanan PHP di Coddy. Pelajaran 60 dari 71.

challenge icon

Tantangan

Mudah

Pada pelajaran sebelumnya, kamu memfilter tugas berdasarkan status penyelesaiannya. Sekarang, tambahkan penanganan untuk kondisi saat hasil filter kosong.

Baca empat baris input untuk deskripsi tugas, yaitu tiga tugas awal ditambah satu tugas baru. Lalu baca input kelima, berupa bilangan bulat yang menunjukkan indeks tugas yang harus ditandai selesai. Baca input keenam, berupa bilangan bulat yang menunjukkan indeks tugas yang harus dihapus. Terakhir, baca input ketujuh, berupa string completed atau pending untuk memfilter tugas.

Setelah membuat array $todos, menandai tugas yang ditentukan sebagai selesai, dan menghapus tugas pada indeks yang diberikan, filter tugas yang tersisa berdasarkan input filter.

Sebelum menampilkan tugas yang sudah difilter, periksa apakah ada tugas yang cocok dengan filter:

  • Jika tidak ada tugas yang cocok dengan filter, cetak: No [filter] tasks found. (dengan [filter] berupa completed atau pending)
  • Jika ada tugas yang cocok dengan filter, tampilkan dengan format yang sama seperti sebelumnya

Format output untuk tugas yang cocok:

  • Jika tugas selesai: - [task description] (Done)
  • Jika tugas belum selesai: - [task description] (Pending)

Contoh 1:

Jika inputnya adalah Buy groceries, Walk the dog, Finish homework, Call mom, 1, 1, dan completed, outputnya seharusnya:

No completed tasks found.

Contoh 2:

Jika inputnya adalah Buy groceries, Walk the dog, Finish homework, Call mom, 0, 2, dan completed, outputnya seharusnya:

- Buy groceries (Done)

Contoh 3:

Jika inputnya adalah Buy groceries, Walk the dog, Finish homework, Call mom, 0, 1, dan pending, outputnya seharusnya:

- Finish homework (Pending)
- Call mom (Pending)

Coba sendiri

<?php
// Read three task descriptions
$task1 = trim(fgets(STDIN));
$task2 = trim(fgets(STDIN));
$task3 = trim(fgets(STDIN));

// Read the new task to add
$newTask = trim(fgets(STDIN));

// Read the index of the task to mark as complete
$completeIndex = (int)trim(fgets(STDIN));

// Read the index of the task to remove
$removeIndex = (int)trim(fgets(STDIN));

// Read the filter type (completed or pending)
$filter = trim(fgets(STDIN));

// Create a $todos array containing three task arrays
// Each task should be an associative array with "task" and "completed" keys
$todos = [
    ["task" => $task1, "completed" => false],
    ["task" => $task2, "completed" => false],
    ["task" => $task3, "completed" => false]
];

// Add the new task to the end of the array
$todos[] = ["task" => $newTask, "completed" => false];

// Mark the task at the specified index as complete
$todos[$completeIndex]["completed"] = true;

// Remove the task at the specified removal index
array_splice($todos, $removeIndex, 1);

// Loop through the array and print each task that matches the filter
foreach ($todos as $todo) {
    if ($filter === "completed" && $todo["completed"]) {
        echo "- " . $todo["task"] . " (Done)\n";
    } elseif ($filter === "pending" && !$todo["completed"]) {
        echo "- " . $todo["task"] . " (Pending)\n";
    }
}
?>

Semua pelajaran di Dasar-Dasar

Latihan sendiri: Compiler PHP online