-
-
Notifications
You must be signed in to change notification settings - Fork 159
London | 25-ITP-Sept | Samuel Tarawally | Sprint 2 | Book Library #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
6f94152 to
a815cee
Compare
…ructure and titles
a815cee to
1582ca0
Compare
cjyuan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good start!
| const storedLibrary = localStorage.getItem("myLibrary"); | ||
| if (storedLibrary) { | ||
| myLibrary = JSON.parse(storedLibrary); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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