Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reading-list-manager/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
4 changes: 4 additions & 0 deletions reading-list-manager/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"semi": true
}
16 changes: 14 additions & 2 deletions reading-list-manager/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// This is the entrypoint for your application.
// node app.js
import {
loadBooks,
printAllBooks,
printSummary,
getBooksByGenre,
getUnreadBooks,
markAsRead,
} from './readingList.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

console.log('📚 MY READING LIST 📚\n');

// Your implementation here
const books = loadBooks();
printAllBooks();
printSummary();
console.log('\nFiction Books:', getBooksByGenre('Fiction'));
console.log('\nUnread Books:', getUnreadBooks());
markAsRead(1);
38 changes: 37 additions & 1 deletion reading-list-manager/books.json
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": "Dune",
"author": "Frank Herbert",
"genre": "Sci-Fi",
"read": false
},
{
"id": 3,
"title": "The Hobbit",
"author": "J.R.R. Tolkien",
"genre": "Fantasy",
"read": true
},
{
"id": 4,
"title": "Sapiens",
"author": "Yuval Noah Harari",
"genre": "Non-fiction",
"read": false
},
{
"id": 5,
"title": "Clean Code",
"author": "Robert C. Martin",
"genre": "Programming",
"read": true
}
]
86 changes: 86 additions & 0 deletions reading-list-manager/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions reading-list-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"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": "module",
"dependencies": {
"chalk": "^4.1.2"
}
}
74 changes: 72 additions & 2 deletions reading-list-manager/readingList.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,123 @@
// Place here the file operation functions for loading and saving books
import fs from 'node:fs';
import chalk from 'chalk';

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 {
if (!fs.existsSync('books.json')) {
return [];
}
const data = fs.readFileSync('books.json', 'utf8');
if (!data.trim()) {
return [];
}
return JSON.parse(data);
} catch (error) {
console.log(chalk.red('Error loading 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.json', JSON.stringify(books, null, 2), 'utf8');
} catch (error) {
console.log(chalk.red('Error saving books.json'));
}
}

function addBook(book) {
// TODO: Implement this function
const books = loadBooks();
const nextId = books.length ? Math.max(...books.map((b) => b.id)) + 1 : 1;
const newBook = { id: nextId, read: false, ...book };
books.push(newBook);
saveBooks(books);
console.log(chalk.green('Added book:'), chalk.cyan(newBook.title));
}

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 updated = books.map((book) =>
book.id === id ? { ...book, read: true } : book,
);
saveBooks(updated);
console.log(chalk.green(`Book ${id} marked as read`));
}

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

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(chalk.bold('\n📚 MY READING LIST 📚\n'));
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(chalk.bold('\n📊 SUMMARY 📊'));
console.log(chalk.bold('Total Books:'), total);
console.log(chalk.bold('Read:'), readCount);
console.log(chalk.bold('Unread:'), unreadCount);
}
export {
loadBooks,
saveBooks,
addBook,
getUnreadBooks,
getBooksByGenre,
markAsRead,
getTotalBooks,
hasUnreadBooks,
printAllBooks,
printSummary,
};