Function to Add a Task
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 48 of 80.
Challenge
EasyAdd a function that puts a new task on the list.
- Delete the lines under
-- Try it. - Above it, write
local function addTask(list: {Task}, title: string): Task: insert{id = #list + 1, title = title, done = false}intolistand return it. - Under
-- Try it, printaddTask(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
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions7Project: Typed Task Manager
Project: The Task ShapeFunction to Add a Task2Core Types
Number, String and BooleanThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesReading a GridBuilding a Gridtable.unpackRecap: Arrays and MapsPractice on your own: Online Luau compiler