generated from HackYourFuture/core-assignment-week-6
-
Notifications
You must be signed in to change notification settings - Fork 18
MohammedA #17
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
Open
mohammedalfakih-dev
wants to merge
1
commit into
HackYourAssignment:main
Choose a base branch
from
mohammedalfakih-dev:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
MohammedA #17
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "tabWidth": 2 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,44 @@ | ||
| [] | ||
| [ | ||
| { | ||
| "id": 1, | ||
| "title": "1984", | ||
| "author": "George Orwell", | ||
| "genre": "Fiction", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 2, | ||
| "title": "Dune", | ||
| "author": "Frank Herbert", | ||
| "genre": "Sci-Fi", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 3, | ||
| "title": "The Hobbit", | ||
| "author": "J.R.R. Tolkien", | ||
| "genre": "Fantasy", | ||
| "read": false | ||
| }, | ||
| { | ||
| "id": 4, | ||
| "title": "Sapiens", | ||
| "author": "Yuval Noah Harari", | ||
| "genre": "History", | ||
| "read": true | ||
| }, | ||
| { | ||
| "id": 5, | ||
| "title": "Atomic Habits", | ||
| "author": "James Clear", | ||
| "genre": "Self-help", | ||
| "read": false | ||
| }, | ||
| { | ||
| "id": 1771455250112, | ||
| "title": "The Catcher in the Rye", | ||
| "author": "J.D. Salinger", | ||
| "genre": "Fiction", | ||
| "read": false | ||
| } | ||
| ] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,119 @@ | ||
| // Place here the file operation functions for loading and saving books | ||
| import fs from 'fs'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| function loadBooks() { | ||
| const filePath = './books.json'; | ||
|
|
||
| export 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 the file does not exist, return an empty array | ||
| if (!fs.existsSync(filePath)) { | ||
| console.log( | ||
| chalk.yellow('⚠ books.json not found. Starting with empty list.'), | ||
| ); | ||
| return []; | ||
| } | ||
|
|
||
| const data = fs.readFileSync(filePath, 'utf-8'); | ||
|
|
||
| return JSON.parse(data); | ||
| } catch (error) { | ||
| console.log( | ||
| chalk.red('❌ Error loading books. Invalid JSON or file issue.'), | ||
| ); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| function saveBooks(books) { | ||
| export function saveBooks(books) { | ||
| // TODO: Implement this function | ||
| // Write books array to books.json | ||
| // Use try-catch for error handling | ||
| try { | ||
| const jsonData = JSON.stringify(books, null, 2); | ||
|
|
||
| fs.writeFileSync(filePath, jsonData); | ||
|
|
||
| console.log(chalk.green('✅ Books saved successfully!')); | ||
| } catch (error) { | ||
| console.log(chalk.red('❌ Error saving books to file.')); | ||
| } | ||
| } | ||
|
|
||
| function addBook(book) { | ||
| export function addBook(book) { | ||
| // TODO: Implement this function | ||
| const books = loadBooks(); | ||
| books.push(book); | ||
| saveBooks(books); | ||
| } | ||
|
|
||
| function getUnreadBooks() { | ||
| export function getUnreadBooks() { | ||
| // TODO: Implement this function using filter() | ||
| return loadBooks().filter((book) => book.read === false); | ||
| } | ||
|
|
||
| function getBooksByGenre(genre) { | ||
| export function getBooksByGenre(genre) { | ||
| // TODO: Implement this function using filter() | ||
| return loadBooks().filter((book) => book.genre === genre); | ||
| } | ||
|
|
||
| function markAsRead(id) { | ||
| export 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); | ||
| } | ||
|
|
||
| function getTotalBooks() { | ||
| export function getTotalBooks() { | ||
| // TODO: Implement this function using length | ||
| return loadBooks().length; | ||
| } | ||
|
|
||
| function hasUnreadBooks() { | ||
| export function hasUnreadBooks() { | ||
| // TODO: Implement this function using some() | ||
| return loadBooks().some((book) => book.read === false); | ||
| } | ||
|
|
||
| function printAllBooks() { | ||
| export 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() { | ||
| export 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.green(`Read: ${readCount}`)); | ||
| console.log(chalk.yellow(`Unread: ${unreadCount}\n`)); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note that this will only match if the casing matches. I would've put everything lowercase for the check.