Adding a Potion
Part of the Fundamentals section of Coddy's Lua journey — lesson 78 of 90.
Challenge
EasyBuilding on your empty inventory table from the previous lesson, create your first magical potion and add it to the shop's inventory. Create a dictionary-style table named healthPotion with the following properties:
namewith the value"Health Potion"pricewith the value15stockwith the value8
After creating the potion table, use table.insert() to add the healthPotion table as the first element in your inventory table. This creates a nested table structure where your inventory contains potion tables.
Finally, print the length of the inventory table to confirm that one potion has been successfully added to your shop.
Try it yourself
-- TODO: Write your code here
inventory = {}
-- Print the length of the inventory table
print(#inventory)All 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 Chest10Tables Part 1: List-Style
What is a Table?Creating a TableAccessing Elements by IndexModifying Elements by IndexThe Length Operator '#'table.insert()table.remove()Recap - Manage Party Members13Project: Simple Potion Shop
Project SetupAdding a Potion2Variables 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