Menu
Coddy logo textTech

Recap - Simple Database

Part of the Fundamentals section of Coddy's Lua journey — lesson 89 of 90.

challenge icon

Challenge

Easy

Create a user database system that combines list-style tables, dictionary-style tables, functions, and loops to search for users by their ID.

First, create a list-style table named users containing three user records. Each user should be a dictionary-style table with the following structure:

  • User 1: id = 101, name = "Alice"
  • User 2: id = 205, name = "Bob"
  • User 3: id = 350, name = "Charlie"

Next, create a function named findUser that takes one parameter userId. The function should:

  • Use a for loop to iterate through the users table
  • Check if any user's id matches the provided userId
  • Return the user's name if found
  • Return nil if no matching user is found

Finally, test your function by calling it three times and printing the results:

  • Search for user ID 205
  • Search for user ID 101
  • Search for user ID 999 (this user doesn't exist)

Try it yourself

-- Create the users table (list-style table containing dictionary-style tables)
-- TODO: Write your code here to create the users table with the three user records

-- Create the findUser function
-- TODO: Write your code here to create the findUser function that searches by userId

-- Test the function and print results
-- TODO: Write your code here to call findUser three times and print the results
-- Remember to search for IDs: 205, 101, and 999

All lessons in Fundamentals