Menu
CoddyTech

Function to Add a Task

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 48 of 80.

challenge icon

Challenge

Easy

Add a function that puts a new task on the list.

  1. Delete the lines under -- Try it.
  2. Above it, write local function addTask(list: {Task}, title: string): Task: insert {id = #list + 1, title = title, done = false} into list and return it.
  3. Under -- Try it, print addTask(tasks, "Review the code").id.

Press Run Code: the output shows 4.

Try it yourself

-- The shape every task in the manager follows
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},
}
-- Try it
print(tasks[1].title)
print(#tasks)
print(tasks[3].done)

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler