Repository navigation
London | 26-ITP-Sep | Shirin Panahian| Sprint 3 | Build a todo-list app - #1572
shirinpanahian wants to merge 5 commits into
Conversation
| Todos.deleteCompleted(todos); | ||
| expect(todos).toHaveLength(lengthBeforeDeletion - lengthOfCompletedTask); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
| // In `script.js`, call `deleteCompleted()` whenever the new button is clicked. | ||
| // - ⚠️ You should not need to modify the `render()` function. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Good catch! The page loads script.mjs, so I removed the incorrect script.js reference from the instructions.
|
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. |
hackertainment
left a comment
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
for your future reference, instead of using .quertSelector("#, it would be clearer to use .getElementById(". Thank you.
| for (let i = 0; i < todos.length; i++) { | ||
| if (todos[i].completed === true) { | ||
| todos.splice(i, 1); | ||
| i--; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
thank you for mention , I replaced the for loop with the filter() method to make the code cleaner and more readable
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
| 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)); | ||
| }); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
|
Well done for all fixes. After you answered #1572 (comment) correctly, I will mark this PR as done. Thank you. |
c58d328 to
bba37fd
Compare
|
Congratulation, you have completed this PR successfully. Please note my additional comment in #1572 (comment) , please keep it up and thank you. |
Learners, PR Template
Self checklist
Task code
CYF-1097
Changelist
deleteCompleted()to remove all completed tasks.