โ ๐ Lua World
๐ Inventory Check
You can practise without a player, but pick a player to score points!
๐ The mission
Today you unlock
Functions return answers
A function is a tiny machine: it receives inputs, works something out, then returns one answer for the tests to check.
๐ก Hint ladder
- Loop the table: for _, thing in ipairs(inventory) do
- If thing == item, return true right away.
- After the loop finishes finding nothing, return false.
๐งช Coach checks
- โฏ finds the key
- โฏ no shield here
- โฏ empty bag
- โฏ first item counts
Live coach
Getting ready
โโโ
๐ฌ Loop through the table; return true the moment you find it. After the loop = not found.
function findNumber(numbers, wanted)
for _, n in ipairs(numbers) do
if n == wanted then
return "found it!"
end
end
return "not here"
end
print(findNumber({3, 7, 9}, 7)) -- found it!
print(findNumber({3, 7, 9}, 5)) -- not here
๐ It printed:
Nice โ you saw it work!
๐ Output
๐งฉ Gaps filled! Ready to write it all yourself?