-
Notifications
You must be signed in to change notification settings - Fork 19
Hamed R. #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Hamed R. #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "singleQuote": true, | ||
| "semi": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,39 @@ | ||
| // This is the entrypoint for your application. | ||
| // node app.js | ||
|
|
||
| // TODO: Implement the main application logic here | ||
| // 1. Load books on startup | ||
| // 2. Display all books | ||
| // 3. Show summary statistics | ||
| // 4. Add example of filtering by genre or read/unread status | ||
| // 5. Add example of marking a book as read | ||
| const readingList = require('./readingList'); | ||
|
|
||
| console.log('📚 MY READING LIST 📚\n'); | ||
|
|
||
| // Your implementation here | ||
| readingList.printAllBooks(); | ||
|
|
||
| readingList.printSummary(); | ||
|
|
||
| const fantasyBooks = readingList.getBooksByGenre('Fantasy'); | ||
| if (fantasyBooks.length > 0) { | ||
| console.log('\n📚 Filter: Fantasy genre 📚'); | ||
| fantasyBooks.forEach((book, index) => { | ||
| const status = book.read ? '✓ Read' : '⚠ Unread'; | ||
| console.log( | ||
| `${index + 1}. ${book.title} by ${book.author} (${book.genre}) ${status}`, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| const unreadBooks = readingList.getUnreadBooks(); | ||
| if (unreadBooks.length > 0) { | ||
| console.log('\n📚 Filter: Unread books 📚'); | ||
| unreadBooks.forEach((book, index) => { | ||
| console.log( | ||
| `${index + 1}. ${book.title} by ${book.author} (${book.genre}) ⚠ Unread`, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| if (unreadBooks.length > 0) { | ||
| const bookToMark = unreadBooks[0]; | ||
| readingList.markAsRead(bookToMark.id); | ||
|
|
||
| console.log( | ||
| `\nMarked as read: ${bookToMark.title} by ${bookToMark.author} ✓`, | ||
| ); | ||
| } | ||
|
|
||
| readingList.printSummary(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,37 @@ | ||
| [] | ||
| [ | ||
| { | ||
| "id": 1, | ||
| "title": "1984", | ||
| "author": "George Orwell", | ||
| "genre": "Fiction", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 2, | ||
| "title": "Sapiens", | ||
| "author": "Yuval Noah Harari", | ||
| "genre": "Non-Fiction", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 3, | ||
| "title": "The Hobbit", | ||
| "author": "J.R.R. Tolkien", | ||
| "genre": "Fantasy", | ||
| "read": false | ||
| }, | ||
| { | ||
| "id": 4, | ||
| "title": "Clean Code", | ||
| "author": "Robert C. Martin", | ||
| "genre": "Programming", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 5, | ||
| "title": "Pride and Prejudice", | ||
| "author": "Jane Austen", | ||
| "genre": "Romance", | ||
| "read": false | ||
| } | ||
| ] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "name": "reading-list-manager", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "app.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "commonjs", | ||
| "dependencies": { | ||
| "chalk": "^4.1.2" | ||
| }, | ||
| "devDependencies": { | ||
| "prettier": "^3.8.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,105 @@ | ||
| // Place here the file operation functions for loading and saving books | ||
| const fs = require('fs'); | ||
| const chalk = require('chalk'); | ||
|
|
||
| const BOOKS_FILE = 'books.json'; | ||
|
|
||
| // File operations | ||
|
|
||
| function loadBooks() { | ||
| // TODO: Implement this function | ||
| // Read from books.json | ||
| // Handle missing file (create empty array) | ||
| // Handle invalid JSON (notify user, use empty array) | ||
| // Use try-catch for error handling | ||
| try { | ||
| const dataBuffer = fs.readFileSync(BOOKS_FILE); | ||
| const dataJSON = dataBuffer.toString(); | ||
| return JSON.parse(dataJSON); | ||
| } catch (e) { | ||
| if (e.code === 'ENOENT') { | ||
| return []; | ||
| } else { | ||
| console.log(chalk.red('Error reading books.json, using empty list')); | ||
| return []; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function saveBooks(books) { | ||
| // TODO: Implement this function | ||
| // Write books array to books.json | ||
| // Use try-catch for error handling | ||
| try { | ||
| fs.writeFileSync(BOOKS_FILE, JSON.stringify(books, null, 2)); | ||
| } catch (e) { | ||
| console.log(chalk.red('Error saving books:', e.message)); | ||
| } | ||
| } | ||
|
|
||
| // Array methods | ||
|
|
||
| function addBook(book) { | ||
| // TODO: Implement this function | ||
| const books = loadBooks(); | ||
| books.push(book); | ||
| saveBooks(books); | ||
|
Comment on lines
33
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You implemented |
||
| } | ||
|
|
||
| function getUnreadBooks() { | ||
| // TODO: Implement this function using filter() | ||
| return loadBooks().filter((book) => !book.read); | ||
| } | ||
|
|
||
| function getBooksByGenre(genre) { | ||
| // TODO: Implement this function using filter() | ||
| return loadBooks().filter( | ||
| (book) => book.genre.toLowerCase() === genre.toLowerCase(), | ||
| ); | ||
| } | ||
|
|
||
| function markAsRead(id) { | ||
| // TODO: Implement this function using map() | ||
| const books = loadBooks(); | ||
| const updatedBooks = books.map((book) => { | ||
| if (book.id === id) { | ||
| return { ...book, read: true }; | ||
| } | ||
| return book; | ||
| }); | ||
| saveBooks(updatedBooks); | ||
| } | ||
|
|
||
| function getTotalBooks() { | ||
| // TODO: Implement this function using length | ||
| return loadBooks().length; | ||
| } | ||
|
|
||
| function hasUnreadBooks() { | ||
| // TODO: Implement this function using some() | ||
| return loadBooks().some((book) => !book.read); | ||
| } | ||
|
|
||
| // Display functions | ||
|
|
||
| function printAllBooks() { | ||
| // TODO: Implement this function | ||
| // Loop through and display with chalk | ||
| // Use green for read books, yellow for unread | ||
| // Use cyan for titles | ||
| const books = loadBooks(); | ||
| console.log('All Books:'); | ||
| books.forEach((book, index) => { | ||
| const status = book.read ? chalk.green('✓ Read') : chalk.yellow('⚠ Unread'); | ||
| console.log( | ||
| `${index + 1}. ${chalk.cyan(book.title)} by ${book.author} (${book.genre}) ${status}`, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| function printSummary() { | ||
| // TODO: Implement this function | ||
| // Show statistics with chalk | ||
| // Display total books, read count, unread count | ||
| // Use bold for stats | ||
| } | ||
| const books = loadBooks(); | ||
| const total = books.length; | ||
| const readCount = books.filter((b) => b.read).length; | ||
| const unreadCount = total - readCount; | ||
| console.log('\n📊 SUMMARY 📊'); | ||
| console.log(chalk.bold(`Total Books: ${total}`)); | ||
| console.log(chalk.green(`Read: ${readCount}`)); | ||
| console.log(chalk.yellow(`Unread: ${unreadCount}\n`)); | ||
| } | ||
|
|
||
| // Export functions | ||
|
|
||
| module.exports = { | ||
| loadBooks, | ||
| saveBooks, | ||
| addBook, | ||
| getUnreadBooks, | ||
| getBooksByGenre, | ||
| markAsRead, | ||
| getTotalBooks, | ||
| hasUnreadBooks, | ||
| printAllBooks, | ||
| printSummary, | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small improvement: in
saveBooks, you log errors but don’t return anything. In larger applications, it’s often helpful to returntrue/falseor re-throw the error for better error handling.