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 @@
{
"semi": true,
"singleQuote": true
}
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": false
},
{
"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": "Clean Code",
"author": "Robert C. Martin",
"genre": "Programming",
"read": false
},
{
"id": 5,
"title": "Atomic Habits",
"author": "James Clear",
"genre": "Self-Help",
"read": true
}
]
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": "commonjs",
"dependencies": {
"chalk": "^4.1.2"
},
"devDependencies": {
"prettier": "^3.8.1"
}
}
94 changes: 70 additions & 24 deletions reading-list-manager/readingList.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,99 @@
// Place here the file operation functions for loading and saving books
const fs = require('fs');
const chalk = require('chalk');
Copy link

Choose a reason for hiding this comment

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

You can also use ES modules which is the more modern way to import modules and is supported by Node.

More info: https://www.w3schools.com/nodejs/nodejs_modules_esm.asp


const filePath = './books.json';

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(filePath)) {
return [];
}

const dataBuffer = fs.readFileSync(filePath);
const dataJSON = dataBuffer.toString();
return JSON.parse(dataJSON);
} catch (error) {
console.log(chalk.red('Error reading file. Using empty list.'));
return [];
}
}

function saveBooks(books) {
// TODO: Implement this function
// Write books array to books.json
// Use try-catch for error handling
try {
const dataJSON = JSON.stringify(books, null, 2);
fs.writeFileSync(filePath, dataJSON);
} catch (error) {
console.log(chalk.red('Error saving file.'));
}
}

function addBook(book) {
// TODO: Implement this function
const books = loadBooks();
books.push(book);
saveBooks(books);
}

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

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

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
const books = loadBooks();
return books.length;
}


function hasUnreadBooks() {
// TODO: Implement this function using some()
const books = loadBooks();
return books.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'));

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
}
const books = loadBooks();
const readCount = books.filter((b) => b.read).length;
const unreadCount = books.length - readCount;

console.log(chalk.bold('\n📊 SUMMARY 📊'));
console.log(chalk.bold(`Total Books: ${books.length}`));
console.log(chalk.bold(`Read: ${readCount}`));
console.log(chalk.bold(`Unread: ${unreadCount}`));
}


printAllBooks();
printSummary();
Copy link

Choose a reason for hiding this comment

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

By convention, calling functions should happen in app.js and definitions of functions can be in a file like this.