โ ๐ Lua World
๐ Coin Counter
You can practise without a player, but pick a player to score points!
๐ The mission
Today you unlock
Loops inspect each item
Many levels use a loop: start with a simple value, visit each item, update the answer, then return it at the end.
๐ก Hint ladder
- Loop through a table with: for _, coin in ipairs(coins) do ... end
- Start a total at 0 and add each coin: total = total + coin
๐งช Coach checks
- โฏ three bags
- โฏ no coins is 0
- โฏ one big bag
- โฏ lots of bags
Live coach
Getting ready
โโโ
๐ฌ ipairs walks through a table one item at a time.
function countItems(items)
local count = 0
for _, item in ipairs(items) do
count = count + 1
end
return count
end
print(countItems({"sword", "map", "apple"})) -- 3
๐ It printed:
Nice โ you saw it work!
๐ Output
๐งฉ Gaps filled! Ready to write it all yourself?