Transfer practice · Return the last item or "Empty". The index depends on the sequence length, not a fixed slot.
v3 · Activity 1 of 1 · tables · arrays · indexing · 9 min Runs here
Use a familiar idea in a different task. Read the changed requirement before running.
Return the last item or "Empty". The index depends on the sequence length, not a fixed slot.
Interface: inventoryInfo(items:{string}) -> {first:string,count:number}
| Input | Expected |
|---|---|
Three items{ "A", "B", "C" } | "C" |
Single item{ "A" } | "A" |
Empty{} | "Empty" |
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.