Sheffield | May-2025 | Waleed-Yahay Salih-Taha | Sprint 2 | Book Library#286
Sheffield | May-2025 | Waleed-Yahay Salih-Taha | Sprint 2 | Book Library#286Bluejay600 wants to merge 18 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/cjyuan/Module-Data-Flows/blob/book-library-feedback/debugging/book-library/feedback.md
Doing so can help me speed up the review process. Thanks.
- In addition, can you change the base branch of this PR from CYF's
book-libraryto CYF'smain?
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s |
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Sprint part (Sprint-2) doesn't match expected format (example: 'Sprint 2', without quotes) |
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Sprint part (Data Flow Sprint 2) doesn't match expected format (example: 'Sprint 2', without quotes) |
|
@cjyuan you can now review it, it's ready. |
debugging/book-library/script.js
Outdated
| const pages = document.getElementById("pages"); | ||
| const check = document.getElementById("check"); | ||
|
|
||
| document.getElementById("submitBtn").addEventListener("click", addBook); |
There was a problem hiding this comment.
It is better to group all the code that's to be executed once on page load together in the window.onload callback function (around line 4)
There was a problem hiding this comment.
now I Grouped all setup code in window.onload
debugging/book-library/script.js
Outdated
| if (!title.value.trim() || !author.value.trim() || !pages.value.trim()) { | ||
| alert("Please fill all fields with valide values (no empty spaces)!"); | ||
| return; | ||
| } | ||
| let book = new Book(title.value.trim(), author.value.trim(), pages.value.trim(), check.checked); | ||
| myLibrary.push(book); |
There was a problem hiding this comment.
-
For better performance (reduce number of function calls) and reducing the chance of using raw input accidently, we could stored the pre-processed/sanitized/normalized input in some variables first, and reference the variables in other part of the function.
-
pages.value.trim()is a string, without proper validation, sanitization, and pre-processing, the following page numbers are possible:
There was a problem hiding this comment.
Pre-processed and sanitizes the input before using it multiple times
debugging/book-library/script.js
Outdated
| alert(`You've deleted title: ${book.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.
There was a problem hiding this comment.
Fixed the delete alert order (show confirmation only after deletion succeeds, not before)
debugging/book-library/script.js
Outdated
| const pages = document.getElementById("pages"); | ||
| const check = document.getElementById("check"); |
There was a problem hiding this comment.
Using descriptive and consistent suffixes (like El, Input, Btn, Form, etc.) for variables that store DOM elements can improve code readability and maintainability.
There was a problem hiding this comment.
I have Consistented suffixes for DOM elements (El, Btn, Form, etc.)
… prevent extremely large numbers of pages (e.g., 9999999). checks if author/title length is within reasonable range.
| const titleInputEl = document.getElementById("title"); | ||
| const authorInputEl = document.getElementById("author"); | ||
| const pagesInputEl = document.getElementById("pages"); | ||
| const readCheckEl = document.getElementById("check"); | ||
| const submitBtnEl = document.getElementById("submitBtn"); | ||
| const bookFormEl = document.getElementById("bookForm"); | ||
| const tbodyEl = document.querySelector("#display tbody"); |
There was a problem hiding this comment.
These set of code could be made an exception and place outside window.onload.
Otherwise, the declared variables can only be accessed locally within the callback function.
| alert("Author must be at least 2 characters long."); | ||
| return; | ||
| } | ||
| const pageNum = Number(pages); |
There was a problem hiding this comment.
If you place this line of code at the beginning of this function, you could then reference the value of this variable at lines 37.
| if (!Number.isInteger(pageNum) || pageNum <= 0 || pageNum > 10000) { | ||
| alert("Pages must be a positive whole number (1–10,000)."); | ||
| return; | ||
| } | ||
|
|
||
| function render() { | ||
| let table = document.getElementById("display"); | ||
| let rowsNumber = table.rows.length; | ||
| //delete old table | ||
| for (let n = rowsNumber - 1; n > 0; n-- { | ||
| table.deleteRow(n); | ||
| // Add book | ||
| let book = new Book(title, author, pages, read); | ||
| myLibrary.push(book); | ||
| render(); | ||
| bookFormEl.reset(); | ||
| } |
There was a problem hiding this comment.
Why not use the VSCode extension prettier to auto format/indent the code?
Learners, PR Template
Self checklist
Changelist
this PR is to View a list of books that I've read Add books to a list of books that I've read, Including title, author, number of pages and if I've read.
Questions
Ask any questions you have for your reviewer.