Menu
Coddy logo textTech

Contrassegnare un'attività come completata

Fa parte della sezione Fondamenti del percorso PHP di Coddy. Lezione 57 di 71.

challenge icon

Sfida

Facile

Nella lezione precedente, hai visualizzato tutte le attività con il relativo stato. Ora aggiungi la possibilità di contrassegnare un'attività specifica come completata.

Leggi quattro righe di input con le descrizioni delle attività (tre attività iniziali più una nuova attività, come prima). Poi leggi un quinto input: un numero intero che rappresenta l'indice dell'attività da contrassegnare come completata (a partire da 0).

Dopo aver creato l'array $todos con tutte e quattro le attività, aggiorna il valore "completed" a true per l'attività all'indice specificato.

Quindi scorri l'array e stampa ogni attività nel seguente formato:

  • Se l'attività è completata: - [task description] (Done)
  • Se l'attività è in sospeso: - [task description] (Pending)

Stampa ogni attività su una riga separata.

Esempio:

Se gli input sono Buy groceries, Walk the dog, Finish homework, Call mom e 1, l'output dovrebbe essere:

- Buy groceries (Pending)
- Walk the dog (Done)
- Finish homework (Pending)
- Call mom (Pending)

Se gli input sono Clean room, Read book, Exercise, Cook dinner e 3, l'output dovrebbe essere:

- Clean room (Pending)
- Read book (Pending)
- Exercise (Pending)
- Cook dinner (Done)

Provalo tu

<?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));

// 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];

// Loop through the array and print each task in the formatted list
foreach ($todos as $todo) {
    echo "- " . $todo["task"] . " (Pending)\n";
}
?>

Tutte le lezioni di Fondamenti

Esercitati da solo: Compilatore PHP online