Transfer practice · Replace only an existing slot, without changing length. Return unchanged items when index is outside the current inventory.
v3 · Activity 1 of 1 · table-insert · table-remove · updates · 11 min Runs here
Use a familiar idea in a different task. Read the changed requirement before running.
Replace only an existing slot, without changing length. Return unchanged items when index is outside the current inventory.
Interface: updateInventory(items:{string},removeIndex:number,newItem:string) -> {string}
| Input | Expected |
|---|---|
Replace second{ "A", "B" }, 2, "C" | { "A", "C" } |
No such slot{ "A" }, 2, "C" | { "A" } |
local items = {"Hammer", "Lamp", "Rope"}
table.remove(items, 2)
table.insert(items, "Brush")
return itemsRemoving Lamp shifts Rope to slot 2. Brush is then appended at slot 3.
New syntax: table.insert(), table.remove()
table.insert(items, value) appends to a dense sequence. table.remove(items, index) removes an element and shifts later entries down.
Assigning nil to a middle index leaves a hole, which changes the sequence assumptions used by # and iteration.
Validate the requested index before removing. This activity receives an index and an item to append, then returns the updated sequence. Copies are taught next; this exercise deliberately updates its supplied table.
Select a word or symbol in this authored example. Your editor draft stays unchanged.
items , ,
table.removeitems, 2
table.insertitems,
itemsThis 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.