Skip to content

London | 26-ITP-Sep | Shirin Panahian| Sprint 3 | Build a todo-list app - #1572

Open
shirinpanahian wants to merge 5 commits into
CodeYourFuture:mainfrom
shirinpanahian:feat/todo-list-clean
Open

shirinpanahian wants to merge 5 commits into
CodeYourFuture:mainfrom
shirinpanahian:feat/todo-list-clean

Conversation

@shirinpanahian

Copy link
Copy Markdown

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Task code

CYF-1097

Changelist

  • Implemented deleteCompleted() to remove all completed tasks.
  • Added a click event listener to the Delete Completed button.
  • Updated the todo list display after deleting completed tasks.
  • Added and updated tests for the functionality.

@shirinpanahian shirinpanahian added 📅 Sprint 3 Assigned during Sprint 3 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Nov 21, 2026
Comment thread Sprint-3/todo-list/todos.test.mjs Outdated
Comment on lines +53 to +54
Todos.deleteCompleted(todos);
expect(todos).toHaveLength(lengthBeforeDeletion - lengthOfCompletedTask);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would this length-only assertion report if deleteCompleted removed the same number of incomplete tasks instead?

To "like" or "dislike" this comment, please follow this link

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! The test only checked the number of remaining todos, not whether the correct todos were deleted. I've updated the assertion to verify that completed todos are removed and incomplete todos remain.

Comment thread Sprint-3/todo-list/script.mjs Outdated
Comment on lines +18 to +19
// In `script.js`, call `deleteCompleted()` whenever the new button is clicked.
// - ⚠️ You should not need to modify the `render()` function.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The page loads this module as script.mjs; how does the script.js reference in this instruction line up with the file being edited?

To "like" or "dislike" this comment, please follow this link

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! The page loads script.mjs, so I removed the incorrect script.js reference from the instructions.

@cyf-ai-code-reviewer cyf-ai-code-reviewer Bot added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Nov 21, 2026
@cyf-ai-code-reviewer

Copy link
Copy Markdown

The CYF AI review has left comments. It will only review a PR one time. When you have addressed these comments, please request review again and a volunteer will take a look.

@shirinpanahian shirinpanahian added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Nov 22, 2026
@hackertainment hackertainment added Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Nov 27, 2026

@hackertainment hackertainment left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your coursework. If you would like to improve on programming logic design, please feel free to book a mentored coding session with a volunteer. Please keep it up.



// A callback that reads the task description from an input field and
const dButton = document.querySelector("#Delete-completed-tasks");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for your future reference, instead of using .quertSelector("#, it would be clearer to use .getElementById(". Thank you.

Comment thread Sprint-3/todo-list/todos.mjs Outdated
for (let i = 0; i < todos.length; i++) {
if (todos[i].completed === true) {
todos.splice(i, 1);
i--;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for-loop running index i should not be manually modified inside the loop. It could confuse the computer or may create side effect, and the code would become hard to maintain. There are two common ways to improve this loop. Can you think of a better one to implement this loop more naturally?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for mention , I replaced the for loop with the filter() method to make the code cleaner and more readable

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the filter() built-in function solved the problem, you also avoided the chance to learn from it as well. I mentioned there are two ways to improve your original for-loop "naturally". That means keeping the loop structure, how to not modify the running index manually but producing correct result? Please suggest both ways below as a reply to this comment.

For your reference, the original for-loop is:

  for (let i = 0; i < todos.length; i++) {
    if (todos[i].completed === true) {
      todos.splice(i, 1);
      i--;
    }
  }

(Hint: think about what purpose the i-- logic can achieve initially)

I acknowledge the workload of CYF coursework, and I want you to success as well. Therefore, I care about whether you really learn from it as well. You will need to explain your code in presentations or job interviews when you graduate. Hope you don't mind me encouraging you to think a bit more, and thank you for your understanding.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your encouragement and for pushing me to revisit this exercise instead of just moving on.

I really appreciate it. I actually tried to complete this exercise three times, and although I initially thought I had understood it, going through it again helped me understand the problem much better.
By starting from the last index and moving towards the first, removing an element doesn't affect the indexes

@hackertainment hackertainment Dec 1, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, you got the point. The reason for having i-- in the original for-loop was because deleting an item would also changing the array size and indexes. Without using simple for-loop implementation, the first common way is to use a while-loop. Since while-loop stops when certain conditions are met (unlike for-loop which repeating constant number of times), you can adjust the array index freely (because i becomes just an array index and no longer a running index), such as:

  let i = 0;
  while (i < todos.length) {
    if (todos[i].completed === true) {
      todos.splice(i, 1);
    }
    else {
      i++;
    }
  }

Another common way is what you've already found: using for-loop from last index back to first index. These two approaches does not require modify the running index of for-loop in the middle of looping. Although the second approach is better and easy to understand, it only works for some problems but not all scenarios. So whenever you found there is a need to modify the running index of a for-loop, think of while-loop first and then see whether a reverse for-loop can optimise the solution.

Comment on lines +44 to +55
test("removes all completed ToDos from the given list.", () => {
const todos = createMockTodos();
const lengthBeforeDeletion = todos.length;
let lengthOfCompletedTask = 0;
for (let item of todos) {
if (item.completed === true) {
lengthOfCompletedTask += 1;
}
}
Todos.deleteCompleted(todos);
expect(todos).toEqual(todos.filter((todos) => !todos.completed));
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is correct. How about when all tasks completed or when all tasks incomplete? Please add those two tests as well. Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion! I've added two additional test cases to cover both edge cases: when all tasks are completed (the list should become empty) and when all tasks are incomplete (the list should remain unchanged).

@hackertainment hackertainment added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. labels Nov 27, 2026
@shirinpanahian shirinpanahian added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Nov 28, 2026
@hackertainment

Copy link
Copy Markdown

Well done for all fixes. After you answered #1572 (comment) correctly, I will mark this PR as done. Thank you.

@hackertainment hackertainment added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Nov 29, 2026
@shirinpanahian shirinpanahian added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Nov 29, 2026
@hackertainment

Copy link
Copy Markdown

Congratulation, you have completed this PR successfully. Please note my additional comment in #1572 (comment) , please keep it up and thank you.

@hackertainment hackertainment added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. 📅 Sprint 3 Assigned during Sprint 3 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants