v3 · Activity 5 of 9 · tables · arrays · indexing · 9 min Runs here
A table can store an ordered sequence: {"Hammer", "Lamp"}. The first position is 1, the second is 2. Position 0 is not automatically the first element.
#items gives the length of a dense sequence. Holes make length unreliable, so keep this lesson inventory dense.
inventoryInfo(items) returns {first=the first item or "Empty", count=the dense sequence length}. Correct the index and empty handling. Keep the final return shown in the starter so the checker can call or inspect your result.
Interface: inventoryInfo(items:{string}) -> {first:string,count:number}
| Input | Expected |
|---|---|
Two slots{ "Hammer", "Lamp" } | { first = "Hammer", count = 2 } |
No slots{} | { first = "Empty", count = 0 } |
local items = {"Hammer", "Lamp"}
print(items[1])
print(#items)
return { first = items[1], count = #items }The first item is Hammer and the dense sequence has two elements.
New syntax: {}, [1], #, return { field = value }
A table can store an ordered sequence: {"Hammer", "Lamp"}. The first position is 1, the second is 2. Position 0 is not automatically the first element.
#items gives the length of a dense sequence. Holes make length unreliable, so keep this lesson inventory dense.
A missing index returns nil. An empty sequence has no first item, so the exercise uses the text Empty as its display fallback.
Select a word or symbol in this authored example. Your editor draft stays unchanged.
inventoryInfoitems
first items0 , count items
inventoryInfoThis is a private experiment copy. It does not replace your answer or complete the activity.
These inputs apply when your scratchpad returns the function described by the activity interface.
Your output will appear here.
Replace this code with the starter? Download your draft to keep a copy.
Your output will appear here.
Sign in with RoCreators to save private progress across devices. Community verification is separate.