Skip to content

Yana P.#9

Open
YanaP1312 wants to merge 4 commits intoHackYourAssignment:mainfrom
YanaP1312:main
Open

Yana P.#9
YanaP1312 wants to merge 4 commits intoHackYourAssignment:mainfrom
YanaP1312:main

Conversation

@YanaP1312
Copy link
Copy Markdown

Build a command-line reading list that saves to files using error handling and array methods.

@github-actions

This comment has been minimized.

Copy link
Copy Markdown

@reposman33 reposman33 left a comment

Choose a reason for hiding this comment

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

lots of comments... you try to be complete (which is good) but don't overdo. And chaining array methods (or any method that returns an array like loadBooks()) is an easy catch

Comment thread reading-list-manager/readingList.js Outdated
const filePath = path.join(dataDir, 'books.json');
const defaultArray = [];

function isDataFull(book) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

{id:'2' title:'', author: '', genre:''} is also dataFull but maybe not what you mean...You would have to check if the properties values are actually valid (not '', id is not '2' but 2 etc) Nice that you want to validate the book object but it is not a requiremwent here so I would just not do it.

Comment thread reading-list-manager/readingList.js Outdated
// Use try-catch for error handling
export function loadBooks() {
try {
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if you check with fs.existsSync() if the file exists before reading you can catch that as well, just like you check the !Array.isArray(data)) case.

Comment thread reading-list-manager/readingList.js Outdated
} catch (error) {
if (error.code === 'ENOENT') {
console.log('Books file not found');
fs.writeFileSync(filePath, JSON.stringify(defaultArray, null, 2));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

defaultArray === [] so JSON.stringify(defaultArray, null, 2) needs no 2. Could be even fs.writeFileSync(filePath, JSON.stringify(defaultArray));

Comment thread reading-list-manager/readingList.js Outdated
export function saveBooks(books) {
try {
if (!Array.isArray(books)) {
throw new Error("Books file doesn't contain an array");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you are right to say 'Books file doesn't contain an array' but a common user doesn't know what an array is. I would say something like 'Books file doesn't contain valid content'

Comment thread reading-list-manager/readingList.js Outdated
}

if (!books.every((book) => isDataFull(book))) {
throw new Error('Book must has id, title, author and genre');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ah, that's what isDataFull is for.... kudos for the check. But not really needed here I think. Being lazy isn't always bad ;)

Comment thread reading-list-manager/readingList.js Outdated
const books = loadBooks();

if (books.some((item) => item.id === book.id)) {
throw new Error(`Book with ${book.id} already exists`);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good catch... it could be there already.

// TODO: Implement this function using filter()
// console.log('Get unread books:', getUnreadBooks());

export function getBooksByGenre(genre) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Array methods can be chained. So this can be:

getBooksByGenre() =>
loadBooks().filter((book) => book.genre === genre)

Comment thread reading-list-manager/readingList.js Outdated
const books = loadBooks();
const updateForRead = books.map((book) => {
if (book.id === id) {
return { ...book, read: true };
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

well done... create a new object from the original and just overwrite the read property

// TODO: Implement this function using some()
// console.log('Total books:', getTotalBooks());

export function hasUnreadBooks() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

can.be.chained: loadBooks().books.some((book) => !book.read);

Comment thread package.json
{
"name": "c55-core-week-6",
"version": "1.0.0",
"description": "The week 6 assignment for the HackYourFuture Core program can be found at the following link: https://hub.hackyourfuture.nl/core-program-week-6-assignment",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice... this helps the user of your app

@reposman33 reposman33 added Reviewed This assignment has been reivewed by a mentor and a feedback has been provided and removed Review in progress labels Mar 10, 2026
… remove redundant validation, and apply array method chaining
@github-actions
Copy link
Copy Markdown

📝 HackYourFuture auto grade

Assignment Score: 0 / 100 ✅

Status: ✅ Passed
Minimum score to pass: 0
🧪 The auto grade is experimental and still being improved

Test Details

@YanaP1312
Copy link
Copy Markdown
Author

Hi! Thank you for the feedback — it was really helpful. I went through all your comments and updated the code accordingly:

  • removed the unnecessary isDataFull validation;
  • added a file existence check using fs.existsSync();
  • improved error messages to be more user‑friendly;
  • removed leftover console logs;
  • implemented automatic ID generation for new books;
  • applied array method chaining where appropriate;

I also refactored loadBooks() so it doesn’t overwrite the JSON file when an error occurs, but still normalizes an empty file to [] to keep the data format consistent for the rest of the functions. This prevents data loss while keeping the module stable.

Thanks again for the review — it definitely helped make the code cleaner and more robust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reviewed This assignment has been reivewed by a mentor and a feedback has been provided

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants