London | 26-ITP-Jan | Angela McLeary | Sprint 2 | Book Library#415
London | 26-ITP-Jan | Angela McLeary | Sprint 2 | Book Library#415AngelaMcLeary wants to merge 30 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Can you check if any of this general feedback can help you further improve your code?
https://github.com/CodeYourFuture/Module-Data-Flows/blob/general-review-feedback/debugging/book-library/feedback.md
Doing so can help me speed up the review process. Thanks.
| function populateStorage() { | ||
| if (myLibrary.length == 0) { | ||
| let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); | ||
| let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); |
There was a problem hiding this comment.
Did you notice the third parameter is a string and not a number?
There was a problem hiding this comment.
Hi @cjyuan, Thanks for your feedback. I did not notice that, thanks for pointing it out. Since the input is a string, I have converted the pages to numbers.
debugging/book-library/script.js
Outdated
| if ( | ||
| title.value == null || | ||
| title.value == "" || | ||
| author.value == null || |
There was a problem hiding this comment.
Why compare .value to null? When will .value ever be null?
There was a problem hiding this comment.
Hi @cjyuan, Thank you for your feedback. You are right, .value is never null. It returns a string or "" when empty. So that check was unnecessary. I have also refactored that code block and removed the unnecessary checks.
debugging/book-library/script.js
Outdated
| } else { | ||
| let book = new Book(title.value, title.value, pages.value, check.checked); | ||
| library.push(book); | ||
| let book = new Book(title.value, author.value, pages.value, check.checked); |
There was a problem hiding this comment.
Using raw input value without properly pre-processing them is dangerous.
Here are some input that can "mess up" the app:
title.value = " ";
page.value = "3e-2";
| @@ -1 +1 @@ | |||
| let myLibrary = []; | |||
debugging/book-library/script.js
Outdated
| let readStatus = ""; | ||
| if (myLibrary[i].check == false) { | ||
| readStatus = "Yes"; | ||
| } else { | ||
| readStatus = "No"; | ||
| } else { | ||
| readStatus = "Yes"; | ||
| } | ||
| changeBut.innerText = readStatus; | ||
| changeBtn.innerText = readStatus; |
There was a problem hiding this comment.
This could be a good opportunity to practice using the ? : conditional operator. Can you rewrite the code on lines 88–94 as a single statement?
There was a problem hiding this comment.
Hi @cjyuan, Thank you for your feedback. Good suggestion — I’ve replaced the if statement with a ternary operator for a cleaner single-line expression.
debugging/book-library/script.js
Outdated
| delBut.innerHTML = "Delete"; | ||
| delBut.addEventListener("clicks", function () { | ||
| let deleteBtn = document.createElement("button"); | ||
| deleteBtn.id = i + 5; |
There was a problem hiding this comment.
- Lines 85, 103:
- Are the values assigned to these
idattributes unique? - Is there any need to assign an id attribute to either buttons?
- Are the values assigned to these
There was a problem hiding this comment.
Hi @cjyuan, Thank you for your feedback. Good point - the id attributes were based on the loop index, so they are not guaranteed to be unique after deletions. Also since I'm not using them for DOM selection, so they are unnecessary, so I've removed both (changeBtn.id and delete.id).
debugging/book-library/script.js
Outdated
| deleteCell.appendChild(deleteBtn); | ||
| deleteBtn.className = "btn btn-warning"; | ||
| deleteBtn.innerHTML = "Delete"; | ||
| deleteBtn.addEventListener("click", function (index) { |
There was a problem hiding this comment.
Why introduce the parameter index to the callback function?
There was a problem hiding this comment.
Hi @cjyuan, Thank you for your feedback. That is definitely a mistake on my part. Furthermore the callback doesn't receive a custom index, only the event object. I wasn't even using it, I've deleted it. I have also refactored the code.
| alert(`You've deleted title: ${myLibrary[i].title}`); | ||
| myLibrary.splice(i, 1); | ||
| render(); |
There was a problem hiding this comment.
The alert message is shown before the book is actually deleted; the deletion only occurs after the alert dialog is dismissed. This introduces a risk that the operation may not complete (e.g., if the user closes the browser before dismissing the alert).
In general, it’s better to display a confirmation message only after the associated operation has successfully completed.

Learners, PR Template
Self checklist
Changelist
Debugging of book library app and fixing typos, syntaxes and validation to mention a few.