NW | 25-ITP-Sep | TzeMing Ho | Sprint 2 | book-library-debugging#326
NW | 25-ITP-Sep | TzeMing Ho | Sprint 2 | book-library-debugging#326TzeMingHo wants to merge 4 commits intoCodeYourFuture:mainfrom
Conversation
|
Can you check if any of this general feedback can help you further improve your code? Doing so can help me speed up the review process. Thanks. |
Thank you CJ, The feedback document is very benefitical. I didn't notice that the project I sumbited still got so many bugs. Thank you for letting me know. I have made some improvements. |
There was a problem hiding this comment.
Your book library works, I just used it and it seems okay, @cjyuan do you have any other comments so that I can mark it complete, thanks
cjyuan
left a comment
There was a problem hiding this comment.
Code works. I just have a few suggestions.
|
|
||
| <div id="demo" class="collapse"> | ||
| <div class="form-group"> | ||
| <form class="form-group" id="book-form" onsubmit="handleSubmit(event)" > |
There was a problem hiding this comment.
Could consider attaching event handler to the form in JS (to keep HTML code free of JS code)
There was a problem hiding this comment.
Yes. I have removed the function from the HTML and created an eventListener in the script.
| pages.value == "" | ||
| !titleInput.value.trim() || | ||
| !authorInput.value.trim() || | ||
| pagesInput.value <= 0 |
There was a problem hiding this comment.
Some "weird" numbers (that shouldn't be considered valid page count) could still pass this check. Can you figure out what these numbers are and "handle" them appropriately?
There was a problem hiding this comment.
I tried my best to think of the weird numbers could be some spaces before or after the actual numbers. I added a trim process for the pages value as well. I hope this will help prevent some strange situations.
| } else { | ||
| let book = new Book(title.value, title.value, pages.value, check.checked); | ||
| library.push(book); | ||
| let book = new Book(String(titleInput.value), String(authorInput.value), Number(pagesInput.value), checkInput.checked); |
There was a problem hiding this comment.
.value is always a string. And on line 39, they could still contain leading or trailing spaces.
| for (let n = rowsNumber - 1; n > 0; n-- { | ||
| table.deleteRow(n); | ||
| } | ||
| let tableBody = document.getElementById("display-body") |
There was a problem hiding this comment.
Could consider (in code refactoring stage) change let to const for all variables that are not going to be reassigned a value.
| let pagesValue = Number(pagesInput.value.trim()); | ||
| let read = checkInput.checked; | ||
| if ( | ||
| !titleInput.value.trim() || | ||
| !authorInput.value.trim() || | ||
| pagesInput.value <= 0 | ||
| !titleValue|| | ||
| !authorValue || | ||
| !Number.isInteger(pagesValue) || |
There was a problem hiding this comment.
Trim of pagesinput.value is optional but ensuring pagesValue is an integer would prevent "weird" page count. So, well done!
| const bookForm = document.getElementById("book-form") | ||
| bookForm.addEventListener("submit", (event) => handleSubmit(event)); | ||
|
|
There was a problem hiding this comment.
It is better to group code that is to be executed once (line 52) in one place (to make locating them easier). For example, in the window.onload callback function (lines 3-5).
| </table> | ||
|
|
||
| <script src="script.js"></script> | ||
| <script src="script.js" defer></script> |
There was a problem hiding this comment.
Suggestion: Consider loading script.js as an ES module to isolate its scope from the global context:
<script src="script.js" type="module"></script>
This ensures that variables, functions, and imports in script.js don’t leak into the global namespace, helps prevent naming conflicts, and enables the use of modern JavaScript features like import and export.
- Also, with
type="module",deferis automatically enforced.
There was a problem hiding this comment.
Thank you. It is something useful. Good to know.
Learners, PR Template
Self checklist
Changelist
I have debugged the book library, which now populates the book collection, displays book information correctly, adds books, and deletes books.