Conversation
📝 HackYourFuture auto gradeAssignment Score: 0 / 100 ✅Status: ✅ Passed Test Details |
reposman33
left a comment
There was a problem hiding this comment.
A few comments re: Array methods in readingList.js. Array methods make for short code, easy to read. Use them, otherwise code becomes quickly too complicated to read.
| export function loadBooks() { | ||
| try { | ||
| if (!fs.existsSync(filePath)) { | ||
| fs.writeFileSync(filePath, JSON.stringify([], null, 2)); |
There was a problem hiding this comment.
I would just do JSON.stringify([]). No null or 2
|
|
||
| const books = loadBooks(); | ||
| const duplicate = books.find( | ||
| (b) => b.title.toLowerCase() === book.title.toLowerCase() |
There was a problem hiding this comment.
do the book.lowerCase() outside the loop because it doesn't change with each iteration;
Don't use find(). find() returns the book with the same title but you are not interested in the book. You only want to know if a book with the same title exists. Use
some(b => b.title === book.title);
some() returns true ("yes, there is a book with the same title as what uou want to add") or false ("no, there is no...")
| (b) => b.title.toLowerCase() === book.title.toLowerCase() | ||
| ); | ||
| if (duplicate) { | ||
| console.error(chalk.red(`⚠ Book "${book.title}" already exists.`)); |
There was a problem hiding this comment.
if you use some() you don't have to assign the book to a const duplicate but you can say if(some(b => b.title === book.title) {...}
| } | ||
|
|
||
| book.id = books.length > 0 ? Math.max(...books.map((b) => b.id)) + 1 : 1; | ||
| book.read = book.read ?? false; |
There was a problem hiding this comment.
book.read is a boolean, So it is {read: true} or {read:false} (assuming it has a valid value and is not read:'' for example). No need to assign false to read when it is not true or undefined (undefined is a falsy value).
| const books = loadBooks(); | ||
| let found = false; | ||
|
|
||
| const updated = books.map((b) => { |
There was a problem hiding this comment.
This looks complicated.. maybe
export function markAsRead(id) {
return loadBooks().map(b => b.id === id ? {...b,read: true} : b)
}
works too
|
|
||
| # Dependency directories | ||
| node_modules/ | ||
| .env |
There was a problem hiding this comment.
You don't have an .env file... so no need to list it here.
Complete week 6 task