Sheffield | 26-ITP-Jan | Mahmoud Shaabo | Sprint 2 | Book Library#450
Sheffield | 26-ITP-Jan | Mahmoud Shaabo | Sprint 2 | Book Library#450mahmoudshaabo1984 wants to merge 2 commits intoCodeYourFuture:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
|
Hi @cifarquhar — PR #450 is ready for review. I have fixed all 5 bugs listed in the readme.md file. The app now loads correctly, books can be added and deleted, and the read status toggles as expected. Please let me know if anything needs to be changed. Thank you! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cjyuan
left a comment
There was a problem hiding this comment.
You have fixed all the bugs, but there are still plenty of room you can improve the code.
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.
|
Hi CJ, Thank you for your feedback. I have reviewed your comments and applied the following improvements: script.js
index.html
Please let me know if there is anything else to improve. Thank you, |
| @@ -1,103 +1,136 @@ | |||
| // myLibrary is the main array — like a shelf that holds all book objects | |||
| let myLibrary = []; | |||
There was a problem hiding this comment.
Safer to declare myLibrary using const because it is not supposed to be reassigned a value.
| const titleInput = document.getElementById("title"); | ||
| const authorInput = document.getElementById("author"); | ||
| const pagesInput = document.getElementById("pages"); | ||
| const readCheckbox = document.getElementById("check"); |
There was a problem hiding this comment.
Good practice to declare all shared constants/variables at the beginning of the file.
| myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", "252", true)); | ||
| myLibrary.push( | ||
| new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true) | ||
| ); |
There was a problem hiding this comment.
It is better to use an appropriate data type to represent all data.
Could consider storing "page count" as a value of type "number".
| titleInput.value === "" || | ||
| authorInput.value === "" || | ||
| pagesInput.value === "" |
There was a problem hiding this comment.
-
String input may contain only space characters or leading/trailing space. Ideally the app should not accept a string containing only spaces as a book title or author's name.
-
pageInput.valueis a string and it can be a number in scientific format (e.g. "3e2") -- which may look weird when displayed as a value of page count.
A better and safer approach to deal with user input is to first preprocess/sanitise them, and then store the cleaned values in variables. Thereafter, refer only to the variables for cleaned values consistently throughout the rest of the code.
| button.addEventListener("click", function () { | ||
| // splice(index, 1) removes exactly 1 item at position "index" | ||
| myLibrary.splice(index, 1); | ||
| render(); | ||
| }); |
There was a problem hiding this comment.
The original code has a statement that uses window.alert() to show the title of the deleted book whenever a book is deleted.
| function createReadButton(index) { | ||
| const button = document.createElement("button"); | ||
| button.className = "btn btn-success"; | ||
|
|
||
| // Use textContent instead of innerHTML for safety | ||
| // textContent never runs any HTML or scripts — innerHTML can be dangerous | ||
| // if the text comes from user input | ||
| button.textContent = myLibrary[index].hasBeenRead ? "Yes" : "No"; | ||
|
|
||
| button.addEventListener("click", function () { | ||
| // Flip the read status: true becomes false, false becomes true | ||
| myLibrary[index].hasBeenRead = !myLibrary[index].hasBeenRead; | ||
| render(); | ||
| }); | ||
|
|
||
| return button; | ||
| } |
There was a problem hiding this comment.
I like the way you use function to isolate the logic for creating the the Read and Delete buttons.
| const table = document.getElementById("display"); | ||
| const rowCount = table.rows.length; | ||
|
|
||
| // Delete all rows except the header row (row 0) | ||
| for (let i = rowCount - 1; i > 0; i--) { | ||
| table.deleteRow(i); | ||
| } |
There was a problem hiding this comment.
For better performance (and also simpler code), you can consider clearing <tbody> instead.
What bugs did I fix?
Bug 1 - Website loads but doesn't show any books
The
forloop in therenderfunction was missing a closing parenthesis)before the{. This caused a syntax error that stopped all JavaScript from running.Bug 2 - Error in console when you try to add a book
The
submitfunction was callinglibrary.push(book)but the array is namedmyLibrary. Fixed by using the correct variable name. Also added validation for the author field.Bug 3 - It uses the title name as the author name
In the
submitfunction,title.valuewas used twice. The second argument should beauthor.value.Bug 4 - Delete button is broken
The delete button variable was created as
delButtonbut then referenced asdelButthree times. Also the event name was"clicks"instead of"click". Fixed both issues.Bug 5 - Read status saves the wrong answer
The
ifcondition was inverted.check == falsewas showing "Yes". Fixed socheck == truecorrectly shows "Yes".How to test
index.htmlin the browserHi @cifarquhar — I have fixed all 5 bugs listed in the readme. Please let me know if you have any feedback. Thank you!