Menu
CoddyTech

Funzione per aggiungere un'attività

Fa parte della sezione Introduzione a Luau del percorso Lua di Coddy. Lezione 48 di 80.

challenge icon

Sfida

Facile

Aggiungi una funzione che inserisca una nuova attività nell'elenco.

  1. Elimina le righe sotto -- Try it.
  2. Sopra, scrivi local function addTask(list: {Task}, title: string): Task: inserisci {id = #list + 1, title = title, done = false} in list e restituiscila.
  3. Sotto -- Try it, stampa addTask(tasks, "Review the code").id.

Premi Esegui codice: l'output mostra 4.

Provalo tu

-- La struttura che segue ogni attività nel gestore
type Task = {
    id: number,
    title: string,
    done: boolean,
}
local tasks: {Task} = {
    {id = 1, title = "Learn Luau types", done = false},
    {id = 2, title = "Build a task manager", done = false},
    {id = 3, title = "Write tests", done = true},
}
-- Provalo
print(tasks[1].title)
print(#tasks)
print(tasks[3].done)

Tutte le lezioni di Introduzione a Luau

Esercitati da solo: Compilatore Luau online