Skip to content

Conversation

@Tarawally
Copy link

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

  • View list of books on page load
  • Add books with title, author, pages, and read status
  • Validate input and show alerts for missing fields
  • Remove books from the list

@Tarawally Tarawally force-pushed the feature/book-library branch from 6f94152 to a815cee Compare December 18, 2025 01:00
@Tarawally Tarawally force-pushed the feature/book-library branch from a815cee to 1582ca0 Compare December 18, 2025 01:12
@Tarawally Tarawally added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Data-Flows The name of the module. 📅 Sprint 2 Assigned during Sprint 2 of this module labels Dec 18, 2025
@Tarawally Tarawally changed the title London | 25-ITP-Sept | Samuel Tarawally | Sprint 2 | Book Library London | 25-ITP-Sept | Samuel Tarawally | Sprint 2 | Book Library Dec 18, 2025
@Tarawally Tarawally linked an issue Dec 18, 2025 that may be closed by this pull request
@Tarawally Tarawally removed 📅 Sprint 2 Assigned during Sprint 2 of this module Submit:PR Module-Data-Flows The name of the module. labels Dec 18, 2025
Copy link
Contributor

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start!

Comment on lines 18 to 21
const storedLibrary = localStorage.getItem("myLibrary");
if (storedLibrary) {
myLibrary = JSON.parse(storedLibrary);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Shouldn't myLibrary be 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, myLibrary will 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 in myLibrary.

Copy link
Contributor

@cjyuan cjyuan Dec 19, 2025

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?

Copy link
Author

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();
  }

Copy link
Contributor

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).

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Dec 18, 2025
@Tarawally
Copy link
Author

Here is my refactored code based on the feedback:

Bug Fixes:

  • Fixed localStorage integration so books persist and display on page load
  • Corrected author data mapping
  • Fixed 'Read' status logic
  • Repaired delete functionality

Code Quality Improvements:

  • Input sanitisation with .trim() and validation
  • XSS prevention using textContent
  • Ternary operators for cleaner conditional logic
  • User feedback alerts on delete
  • Form auto-reset after submission
  • Clear table rendering with .forEach()

pagesInput.value,
readCheckbox.checked
);
const book = new Book(title, author, pages, readCheckbox.checked);
Copy link
Contributor

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.

Copy link
Author

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);

Copy link
Contributor

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?

Copy link
Author

@Tarawally Tarawally Dec 21, 2025

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)) {}

Copy link
Author

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) {}

Comment on lines 18 to 21
const storedLibrary = localStorage.getItem("myLibrary");
if (storedLibrary) {
myLibrary = JSON.parse(storedLibrary);
}
Copy link
Contributor

@cjyuan cjyuan Dec 19, 2025

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?

@Tarawally
Copy link
Author

I've updated populateStorage to seed the library with default data if localStorage is empty. To ensure consistency, I used Array.prototype.map() to convert the plain objects returned by JSON.parse() back into Book instances.

@Tarawally
Copy link
Author

I implemented explicit type conversion for the page count using the Number() function. I also added a guard clause using isNaN() to ensure only valid, positive numbers are accepted.

@Tarawally Tarawally added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. 📅 Sprint 2 Assigned during Sprint 2 of this module and removed Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Dec 21, 2025
@Tarawally Tarawally added Module-Data-Flows The name of the module. Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Dec 21, 2025
Copy link
Contributor

@cjyuan cjyuan left a 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.

Comment on lines 18 to 21
const storedLibrary = localStorage.getItem("myLibrary");
if (storedLibrary) {
myLibrary = JSON.parse(storedLibrary);
}
Copy link
Contributor

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).

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Dec 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Data-Flows The name of the module. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TECH ED] Book Library

2 participants