Conversation
📝 HackYourFuture auto gradeAssignment Score: 0 / 100 ✅Status: ✅ Passed Test Details |
reposman33
left a comment
There was a problem hiding this comment.
Lots of comments, some more important than others. Just sift through it and focus on the array methods. Kudos for elaborate package.json and error checking when reading file.
| @@ -0,0 +1,24 @@ | |||
| { | |||
There was a problem hiding this comment.
Very elaborate package,json file. Good attention to detail!
| const content = fs.readFileSync('./books.json', 'utf8'); | ||
| const books = JSON.parse(content); | ||
|
|
||
| if (!Array.isArray(books)) { |
| // Handle invalid JSON (notify user, use empty array) | ||
| // Use try-catch for error handling | ||
| try { | ||
| const content = fs.readFileSync('./books.json', 'utf8'); |
There was a problem hiding this comment.
Better to check first if file exsts - If yes: then read file. Then check if read content is an array. If yes: continue. Else return empty array
| // Handle invalid JSON (notify user, use empty array) | ||
| // Use try-catch for error handling | ||
| try { | ||
| const content = fs.readFileSync('./books.json', 'utf8'); |
There was a problem hiding this comment.
fs.readFileSync('./books.json', 'utf8'); can use FILE_NAME too
| } | ||
| if (error instanceof SyntaxError) { | ||
| console.log( | ||
| chalk.red( |
There was a problem hiding this comment.
Nice to check for specific errors and inform the user
| // TODO: Implement this function using map() | ||
| const updatedBooks = books.map(function (book) { | ||
| if (book.id === id) { | ||
| return { |
There was a problem hiding this comment.
I saw this elsewhere:
return {...books, read: true}.
Saves writing out a whole new object. You just create a new object from the existing object and overwrite the read property with another value
|
|
||
| //Does book exist? | ||
|
|
||
| const oldBook = books.find(function (book) { |
There was a problem hiding this comment.
books.find(book => book.id === id) does the same because you don't use a function expression (function(book)...). Devs don't use function expressions in array methods
| function hasUnreadBooks() { | ||
| function hasUnreadBooks(books) { | ||
| // TODO: Implement this function using some() | ||
| const hasUnread = books.some(function (book) { |
| return; | ||
| } | ||
|
|
||
| for (let i = 0; i < books.length; i++) { |
There was a problem hiding this comment.
books.forEach(book => {const status = book.read ? chalk.green('✓ Read') : chalk.red('○ Unread'))
console.log(...)
}
is less code and does the same
|
|
||
| // Count read books | ||
| let readCount = 0; | ||
| for (let i = 0; i < books.length; i++) { |
There was a problem hiding this comment.
const readCount = books.reduce((acc,book) => acc += book.read ? 1 : 0).
Looks a bit complicated but once you get it, it is easier and much less code to remember ('cognitive load')
Week 6 assingment