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
6 changes: 6 additions & 0 deletions reading-list-manager/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "es5"
}
19 changes: 14 additions & 5 deletions reading-list-manager/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// This is the entrypoint for your application.
// node app.js

import {
printAllBooks,
printSummary,
getBooksByGenre,
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
printAllBooks();
printSummary();

console.log('\nGet books by genre:', getBooksByGenre('Fantasy'));

console.log('\nReading update:');
markAsRead(2);
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": "The Hobbit",
"author": "J.R.R. Tolkien",
"genre": "Fantasy",
"read": true
},
{
"id": 2,
"title": "Dune",
"author": "Frank Herbert",
"genre": "Sci-Fi",
"read": true
},
{
"id": 3,
"title": "Pride and Prejudice",
"author": "Jane Austen",
"genre": "Romance",
"read": true
},
{
"id": 4,
"title": "Atomic Habits",
"author": "James Clear",
"genre": "Self-Help",
"read": false
},
{
"id": 5,
"title": "Hunger Games",
"author": "Suzanne Collins",
"genre": "Sci-Fi",
"read": false
}
]
105 changes: 105 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.

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

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
// TODO: Implement this function
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good practice to remove TODO comments after you've implemented them. Keeps the code cleaner and shows more clearly the requirement has been implemented.

// 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 data = fs.readFileSync('books.json', 'utf8');
return JSON.parse(data);
} catch (error) {
console.log('Could not load books. Using empty list.');
return [];
}
}

function saveBooks(books) {
// TODO: Implement this function
// Write books array to books.json
// Use try-catch for error handling
// 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));
} catch (error) {
console.log(chalk.red('Error saving books:', error.message));
}
}

function addBook(book) {
// TODO: Implement this function
// TODO: Implement this function
const books = loadBooks();
books.push(book);
saveBooks(books);
console.log(chalk.green(`Book "${book.title}" added!`));
}

function getUnreadBooks() {
// TODO: Implement this function using filter()
// TODO: Implement this function using filter()
const books = loadBooks();
return books.filter((book) => !book.read);
}

function getBooksByGenre(genre) {
// TODO: Implement this function using filter()
// TODO: Implement this function using filter()
const books = loadBooks();
return books.filter(
(book) => book.genre.toLowerCase() === genre.toLowerCase()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch converting to lower case when comparing strings ⭐
Also a real world scenario that happens often in apps.

);
}

function markAsRead(id) {
// TODO: Implement this function using map()
// TODO: Implement this function using map()
const books = loadBooks();
const updatedBooks = books.map((book) => {
if (book.id === id) book.read = true;
return book;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pay attention to indenting.

});
saveBooks(updatedBooks);
console.log(chalk.green(`Book with ID ${id} marked as read.`));
}

function getTotalBooks() {
// TODO: Implement this function using length
// TODO: Implement this function using length
return loadBooks().length;
}

function hasUnreadBooks() {
// TODO: Implement this function using some()
// 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
// 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 📚'));
books.forEach((book) => {
const status = book.read
? chalk.green('✓ Read')
: chalk.yellow('⚠ Unread');
console.log(
`${book.id}. ${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
}
// 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 read = books.filter((b) => b.read).length;
const unread = total - read;

console.log(chalk.bold('\n📊 SUMMARY 📊'));
console.log(`Total Books: ${total}`);
console.log(`Read: ${read}`);
console.log(`Unread: ${unread}`);
}

export {
loadBooks,
saveBooks,
addBook,
getUnreadBooks,
getBooksByGenre,
markAsRead,
getTotalBooks,
hasUnreadBooks,
printAllBooks,
printSummary,
};