Menu
Coddy logo textTech

Function to Add a Task

Part of the Introduction To Luau section of Coddy's Lua journey — lesson 46 of 73.

challenge icon

Challenge

Easy

Your editor already contains the Task type and the tasks array from the previous lesson.

Create a function named addTask that takes two parameters:

  • list of type {Task}
  • title of type string

The function should:

  • build a new Task whose id is #list + 1, whose title is the given title, and whose done flag is false
  • insert it into list with table.insert
  • return the new task (return type Task)

Then call addTask(tasks, "Review the code"), store the result in a variable named added, and print, each on its own line:

  1. the number of tasks now in tasks
  2. added.id
  3. added.title
  4. added.done

Try it yourself

-- The shape every task in the manager must follow
type Task = {
    id: number,
    title: string,
    done: boolean,
}

-- The task list: a typed array of Task shapes
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},
}

-- Inspect the first task
print(tasks[1].id)
print(tasks[1].title)
print(tasks[1].done)
print(#tasks)

All lessons in Introduction To Luau