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": false
}
33 changes: 23 additions & 10 deletions reading-list-manager/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
// This is the entrypoint for your application.
// node app.js
import {
loadBooks,
saveBooks,
addBook,
getUnreadBooks,
getBooksByGenre,
markAsRead,
getTotalBooks,
hasUnreadBooks,
printAllBooks,
printSummary,
} from "./readingList.js";
Comment on lines +1 to +12

Choose a reason for hiding this comment

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

Some of the imported functions are currently unused in app.js. You can remove unused imports. Keeping imports clean helps maintain readability and avoids confusion.


// 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 newBook = {
id: 6,
title: "Clean Code",
author: "Robert C. Martin",
genre: "Software Development",
read: false,
};

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

// Your implementation here
printAllBooks();
console.log("\n");
printSummary();
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": "Dystopian Fiction",
"read": false
},
{
"id": 2,
"title": "The Diary of a Young Girl",
"author": "Anne Frank",
"genre": "Autobiography",
"read": true
},
{
"id": 3,
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"genre": "Classic Literature",
"read": false
},
{
"id": 4,
"title": "Atomic Habits",
"author": "James Clear",
"genre": "Self-Help",
"read": true
},
{
"id": 5,
"title": "The Hitchhiker's Guide to the Galaxy",
"author": "Douglas Adams",
"genre": "Science Fiction",
"read": false
}
]
28 changes: 28 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.

17 changes: 17 additions & 0 deletions reading-list-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "reading-list-manager",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node ./app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"chalk": "^5.6.2"

Choose a reason for hiding this comment

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

Assignment asks for chalk@4.1.2.
Now your code runs correctly, but it's always good to check for the correct package versions.

}
}
135 changes: 102 additions & 33 deletions reading-list-manager/readingList.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,122 @@
import chalk from "chalk";
import fs from "node:fs";

// 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
export function loadBooks() {
try {
const jsonString = fs.readFileSync("books.json", "utf8");
const books = JSON.parse(jsonString);
return books;
} catch (error) {
if (error.code === "ENOENT") {
const books = [];
return books;
} else if (error.name === "SyntaxError") {
console.log("invalid JSON");
const books = [];
return books;
}
}
}

function saveBooks(books) {
// TODO: Implement this function
// Write books array to books.json
// Use try-catch for error handling
export function saveBooks(books) {
try {
fs.writeFileSync("books.json", JSON.stringify(books, null, 2));
} catch (error) {
if (error.code === "ENOENT") {
console.log("File not exist");
} else if (error.name === "SyntaxError") {
console.log("invalid JSON");
}
}
return true;
Comment on lines +23 to +33

Choose a reason for hiding this comment

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

This function saveBooks() always returns true in line 33 even if writing fails, because return true is outside the try. Better to return true only on success and false in catch.

}

function addBook(book) {
// TODO: Implement this function
export function addBook(book) {
const books = loadBooks();
if (!book.id) {
book.id = books.length + 1;
}
books.push(book);

if (saveBooks(books)) {
return true;
}
return false;
}

function getUnreadBooks() {
// TODO: Implement this function using filter()
export function getUnreadBooks() {
const books = loadBooks();
const unreadBooks = books.filter((book) => book.read === false);

return unreadBooks;
}

function getBooksByGenre(genre) {
// TODO: Implement this function using filter()
export function getReadBooks() {
const books = loadBooks();
const readBooks = books.filter((book) => book.read === true);

return readBooks;
}

function markAsRead(id) {
// TODO: Implement this function using map()
export function getBooksByGenre(genre) {
const books = loadBooks();
const booksByGenre = books.filter((book) => book.genre === genre);

return booksByGenre;
}

function getTotalBooks() {
// TODO: Implement this function using length
export function markAsRead(id) {
const books = loadBooks();
const updateBooks = books.map(function (book) {
if (book.id === id) {
book.read = true;
}
return { ...book };
});

if (saveBooks(updateBooks)) {
return true;
}
return false;
}

function hasUnreadBooks() {
// TODO: Implement this function using some()
export function getTotalBooks() {
const books = loadBooks();
return books.length;
}

function printAllBooks() {
// TODO: Implement this function
// Loop through and display with chalk
// Use green for read books, yellow for unread
// Use cyan for titles
export function hasUnreadBooks() {
const Books = loadBooks();
const unreadBooks = Books.some((book) => book.read === false);

return unreadBooks;
}

function printSummary() {
// TODO: Implement this function
// Show statistics with chalk
// Display total books, read count, unread count
// Use bold for stats
}
export function printAllBooks() {
console.log(chalk.bold("📚 MY READING LIST 📚"));
const books = loadBooks();
for (const book of books) {
const { id, title, author, genre, read } = book;

let readStatus = null;
if (read === true) {
readStatus = chalk.green(`✓ Read`);
} else {
readStatus = chalk.yellow(`⚠ Unread`);
}

console.log(
`${id}. ${chalk.cyan(title)} by ${author} (${genre}) ${readStatus}`,
);
}
}

export function printSummary() {
console.log(chalk.bold("📊 SUMMARY 📊"));

console.log(chalk.bold(`Total Books: ${loadBooks().length}`));
console.log(chalk.green.bold(`Read: ${getReadBooks().length}`));
console.log(chalk.yellow.bold(`Unread: ${getUnreadBooks().length}`));
}