Menu
Coddy logo textTech

Recap: Typed Functions

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

challenge icon

Challenge

Medium

Create a function type alias named NameFormatter for a function that takes two required strings and an optional string?, and returns a string.

Create a function named formatName of type NameFormatter (a local annotated with the alias). It receives firstName, lastName and an optional middleName:

  • When a middle name is provided, return "[firstName] [middleName] [lastName]"
  • Otherwise return "[firstName] [lastName]"

Read three input lines: the first name, the last name, and the middle name — which may be an empty line, meaning there is no middle name.

If the third line is empty (""), call formatName with only the two names; otherwise pass all three. Print the result.

Try it yourself

local firstName: string = io.read()
local lastName: string = io.read()
local middleName: string = io.read()

-- Write code here
-- type NameFormatter = (string, string, string?) -> string
-- local formatName: NameFormatter = ...
-- if middleName is "" call with two args, else with three

All lessons in Introduction To Luau