v3 · Activity 5 of 5 · loops-and-conditions · filtering · 11 min Runs here
A loop visits each objective; an if decides whether that objective belongs in the result. Filtering does not require changing the source sequence.
Start with an empty result and append only unfinished names. Returning the result after the loop handles a fully completed quest as an empty sequence.
remainingObjectives(objectives) returns names for complete=false entries in their original order. The starter selects the wrong state. Keep the final return shown in the starter so the checker can call or inspect your result.
Interface: remainingObjectives(objectives:{{name:string,complete:boolean}}) -> {string}
| Input | Expected |
|---|---|
Mixed quest{ { name = "Lamp", complete = true }, { name = "Gate", complete = false }, { name = "Bench", complete = false } } | { "Gate", "Bench" } |
All complete{ { name = "Lamp", complete = true } } | {} |
local tasks = { {name="Light lamp", complete=true}, {name="Open door", complete=false} }
local remaining = {}
for _, task in ipairs(tasks) do
if not task.complete then table.insert(remaining, task.name) end
end
return remainingThe completed lamp task is skipped; the unfinished door task is appended.
New syntax: if inside a loop
A loop visits each objective; an if decides whether that objective belongs in the result. Filtering does not require changing the source sequence.
Start with an empty result and append only unfinished names. Returning the result after the loop handles a fully completed quest as an empty sequence.
A boolean complete field describes state. Check not objective.complete to select unfinished work. For this lesson every objective supplies a boolean; missing-data policies come next.
Select a word or symbol in this authored example. Your editor draft stays unchanged.
remainingObjectivesobjectives
result
_, objective in objectives
objective.complete table.insertresult, objective.name
result
remainingObjectivesThis 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.