Menu
CoddyTech

Função: Exibir Resumo das Tarefas

Parte da seção Introdução ao Luau do Journey de Lua da Coddy. Lição 51 de 80.

challenge icon

Desafio

Fácil

Adicione uma função que imprime o progresso da lista.

  1. Exclua as linhas abaixo de -- Try it.
  2. Escreva local function printSummary(list: {Task}): () que conta as tarefas concluídas e imprime `{doneCount}/{#list} tasks done`.
  3. Abaixo de -- Try it, chame printSummary(tasks), depois completeTask(tasks, 2) e, em seguida, chame printSummary(tasks) novamente.

Pressione Executar código: a saída mostra 1/3 tasks done e depois 2/3 tasks done.

Experimente você mesmo

-- A estrutura que todas as tarefas do gerenciador seguem
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},
}
-- Crie uma tarefa com o próximo id disponível e adicione-a à lista
local function addTask(list: {Task}, title: string): Task
    local newTask: Task = {id = #list + 1, title = title, done = false}
    table.insert(list, newTask)
    return newTask
end
-- Marque a tarefa com este id como concluída; informe se alguma foi encontrada
local function completeTask(list: {Task}, id: number): boolean
    for _, t in ipairs(list) do
        if t.id == id then
            t.done = true
            return true
        end
    end
    return false
end
-- Uma nova lista das tarefas cujo valor de done corresponde ao solicitado
local function filterTasks(list: {Task}, done: boolean): {Task}
    local result: {Task} = {}
    for _, t in ipairs(list) do
        if t.done == done then
            table.insert(result, t)
        end
    end
    return result
end
-- Experimente
local pending = filterTasks(tasks, false)
print(#pending)
print(pending[2].title)

Todas as lições de Introdução ao Luau

Pratique por conta própria: Compilador de Luau online