Skip to content

Yana P.#9

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

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

Conversation

@YanaP1312
Copy link

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

@github-actions
Copy link

📝 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

Copy link

@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

const filePath = path.join(dataDir, 'books.json');
const defaultArray = [];

function isDataFull(book) {

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.

// Use try-catch for error handling
export function loadBooks() {
try {
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));

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.

} catch (error) {
if (error.code === 'ENOENT') {
console.log('Books file not found');
fs.writeFileSync(filePath, JSON.stringify(defaultArray, null, 2));

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

export function saveBooks(books) {
try {
if (!Array.isArray(books)) {
throw new Error("Books file doesn't contain an array");

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'

}

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

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

const books = loadBooks();

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

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

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)

const books = loadBooks();
const updateForRead = books.map((book) => {
if (book.id === id) {
return { ...book, read: true };

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

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

{
"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",

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