Unit 6 · JavaScript in the page
Removing a clicked item: pop vs splice vs filter
pop() always removes the last item. To remove the one that was clicked, use its index with splice(index, 1), or build a new array with filter. Then call render().
The mix-up
What people often think
“pop() removes the task that was clicked.”
What the tutor says
pop() always removes the last item. Use the button's index with splice, or filter out that task.
How it works
Patching the visible count instead, by subtracting one from the text, leaves the array unchanged, so the next render brings the task back.
filter is often the clearest: keep every item except the one to remove.
Side by side
todos.pop(); // removes the last task, whichever was clickedtodos.splice(index, 1);
// or
todos = todos.filter((t, i) => i !== index);
render();Close cousins of this mix-up
The tutor watches for these in the same lesson.
“The fix is to subtract one from the counter text.”
That patches the symptom, and the array still has three items. Remove the task from todos, then call render().
Practise it with a tutor watching
State
Build a counter and a to-do list where the data decides what the screen shows.
Free with an account. The first two units need no account.