Recap - Simple Database
Part of the Fundamentals section of Coddy's Lua journey — lesson 89 of 90.
Challenge
EasyCreate 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
forloop to iterate through theuserstable - Check if any user's
idmatches the provideduserId - Return the user's
nameif found - Return
nilif 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 999All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator