Menu
Coddy logo textTech

Finding a Specific Potion

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

challenge icon

Challenge

Easy

Building on your inventory value calculation system, implement a search feature that allows you to find and display detailed information about a specific potion in your shop's inventory.

The following input will be provided:

  • A string containing the name of the potion to search for

Use a numeric for loop to iterate through your inventory table. For each potion, check if the potion's name matches the search term using the equality operator ==.

When you find the matching potion, print all of its details in the following format:

Found: [name]
Price: [price] gold
Stock: [stock] units

Use string concatenation to combine the descriptive text with each potion property. After finding and displaying the potion details, use the break statement to exit the loop since you've found what you were looking for.

Try it yourself

-- Inventory table with potion data
local inventory = {
    {name = "Health Potion", price = 15, stock = 8},
    {name = "Mana Potion", price = 12, stock = 5},
    {name = "Strength Potion", price = 25, stock = 3},
    {name = "Speed Potion", price = 18, stock = 2}
}

-- Initialize total value variable
local totalValue = 0

-- Calculate total inventory value using numeric for loop
for i = 1, #inventory do
    local itemValue = inventory[i].price * inventory[i].stock
    totalValue = totalValue + itemValue
end

-- Print the total inventory value
print("Total inventory value: " .. totalValue .. " gold")

All lessons in Fundamentals