London | 25-ITP-Sept | Samuel Tarawally | Sprint 2 | Book Library#364
London | 25-ITP-Sept | Samuel Tarawally | Sprint 2 | Book Library#364Tarawally wants to merge 6 commits intoCodeYourFuture:mainfrom
Conversation
6f94152 to
a815cee
Compare
…ructure and titles
a815cee to
1582ca0
Compare
| const storedLibrary = localStorage.getItem("myLibrary"); | ||
| if (storedLibrary) { | ||
| myLibrary = JSON.parse(storedLibrary); | ||
| } |
There was a problem hiding this comment.
-
Shouldn't
myLibrarybe populated with some data for users who do not have any data in their browser's local storage? -
The objects in the restored array are not instances of Book. A a result,
myLibrarywill end up with two kinds of objects: Generic objects and Book objects (added on line 42). Even though this is not a problem for this simple app, it is a better practice to consistently keep the same type of data inmyLibrary.
There was a problem hiding this comment.
Can you also address the comment I left on the code on lines 18-21 in the previous review?
There was a problem hiding this comment.
To populate data for users who do not have any, and to restore plain data objects back into book objects, the following code snippet has been applied:
const storedLibrary = localStorage.getItem("myLibrary");
if (storedLibrary) {
const rawData = JSON.parse(storedLibrary);
// Rehydrates plain data back into Book objects
myLibrary = rawData.map(
(data) =>
new Book(data.title, data.author, Number(data.pages), data.check)
);
} else {
// Seeds data for new users
myLibrary = [
new Book("The Hobbit", "J.R.R. Tolkien", 295, false),
new Book("1984", "George Orwell", 328, true),
new Book("Robinson Crusoe", "Daniel Defoe", 252, true),
new Book("The Old Man and the Sea", "Ernest Hemingway", 127, false),
];
saveStorage();
}There was a problem hiding this comment.
Well done.
Please note that if pages are consistently stored values of type number, we would not need the conversion Number(data.pages).
|
Here is my refactored code based on the feedback: Bug Fixes:
Code Quality Improvements:
|
| pagesInput.value, | ||
| readCheckbox.checked | ||
| ); | ||
| const book = new Book(title, author, pages, readCheckbox.checked); |
There was a problem hiding this comment.
On line 41, the value of pages can still be a string that represents an invalid number of pages.
Can you ensure the value of pages represents a valid number of pages.
There was a problem hiding this comment.
To address this I've made the following changes:
const pages = Number(pagesInput.value);
// Validates input: checks for empty strings, non-numbers, or negative values
if (!title || !author || isNaN(pages) || pages <= 0) {
alert("Please enter valid book details. Pages must be a positive number.");
return;
}
const book = new Book(title, author, pages, readCheckbox.checked);There was a problem hiding this comment.
Not all positive finite numbers are valid "number of pages" though. What other numbers should also be rejected?
There was a problem hiding this comment.
float would be a problem so i guess this would be better
if (!title || !author || isNaN(pages) || pages <= 0 || !Number.isInteger(pages)) {}There was a problem hiding this comment.
this could also be shorten to
if (!title || !author || !Number.isInteger(pages) || pages <= 0) {}| const storedLibrary = localStorage.getItem("myLibrary"); | ||
| if (storedLibrary) { | ||
| myLibrary = JSON.parse(storedLibrary); | ||
| } |
There was a problem hiding this comment.
Can you also address the comment I left on the code on lines 18-21 in the previous review?
|
I've updated |
|
I implemented explicit type conversion for the page count using the |
cjyuan
left a comment
There was a problem hiding this comment.
Changes look good. I will mark this PR as complete first.
| const storedLibrary = localStorage.getItem("myLibrary"); | ||
| if (storedLibrary) { | ||
| myLibrary = JSON.parse(storedLibrary); | ||
| } |
There was a problem hiding this comment.
Well done.
Please note that if pages are consistently stored values of type number, we would not need the conversion Number(data.pages).
Learners, PR Template
Self checklist
Changelist