v3 · Activity 8 of 9 · table-insert · table-remove · updates · 11 min Runs here
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.
updateInventory(items, removeIndex, newItem) removes an index only when it is within 1..#items, then always appends newItem. Return the updated sequence. Keep the final return shown in the starter so the checker can call or inspect your result. The starter deliberately contains an error; a failing run is useful evidence while you repair it.
Interface: updateInventory(items:{string},removeIndex:number,newItem:string) -> {string}
| Input | Expected |
|---|---|
Remove middle and append{ "Hammer", "Lamp", "Rope" }, 2, "Brush" | { "Hammer", "Rope", "Brush" } |
Invalid index{ "Hammer" }, 4, "Lamp" | { "Hammer", "Lamp" } |
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.