Transfer practice · Compare two item tables by their name and quantity fields, not their identities. Return false when either field differs.
v3 · Activity 1 of 1 · table-identity · references · nested-tables · 12 min Runs here
Use a familiar idea in a different task. Read the changed requirement before running.
Compare two item tables by their name and quantity fields, not their identities. Return false when either field differs.
Interface: previewLoadout(saved:{tools:{string}},extra:string) -> summary table
| Input | Expected |
|---|---|
Equal-looking separate tables{ name = "Lamp", quantity = 2 }, { name = "Lamp", quantity = 2 } | true |
Different quantity{ name = "Lamp", quantity = 2 }, { name = "Lamp", quantity = 3 } | false |
local saved = { tools = {"Hammer"} }
local preview = table.clone(saved)
preview.tools = table.clone(saved.tools)
table.insert(preview.tools, "Brush")
return { originalCount = #saved.tools, previewCount = #preview.tools }Copying both the outer table and the tools sequence lets the preview change independently.
New syntax: table.clone()
Your explanation is a self-check. Exact wording is not automatically graded.
Assigning local preview = original gives both names the same table. It does not duplicate contents. Two separately created tables with equal-looking contents are different identities.
table.clone makes a shallow copy. Nested tables inside the copy still refer to the original nested tables.
Copy only the nested layer you plan to change for this activity. Arbitrary deep copying needs policies for cycles, functions and special engine values; this small example does not claim to solve all of them.
Select a word or symbol in this authored example. Your editor draft stays unchanged.
previewLoadoutsaved, extra
preview table.clonesaved
table.insertpreview.tools, extra
originalCount saved.tools, previewTools preview.tools
previewLoadoutThis 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.