Skip to content
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

Humoud AlGhanim #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion src/challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function addReview(
comment: string
): ReviewedBook {
// write your code here...

book.reviews
? book.reviews.push({ reviewer: reviewer, comment: comment })
: (book.reviews = [{ reviewer: reviewer, comment: comment }]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ternary operators should only really be used as expressions (where the entire ternary operator will be replaced with a value. They should not have side-effects or be used as a general replacement for if statements.

A better ternary use here would be:

  const newReview: Review = { reviewer, comment };

  book.reviews =
    book.reviews ? [...book.reviews, newReview] : [newReview];

return book;
}

Expand Down
25 changes: 16 additions & 9 deletions src/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ function createBook(
genre: string
): Book {
// write your code here...

return {} as Book; // replace "{} as Book" with what you see is fit
return {
title: title,
author: author,
publishedYear: publishedYear,
genre: genre,
}; // replace "{} as Book" with what you see is fit
Copy link
Contributor

@Octowl Octowl Mar 9, 2025

Choose a reason for hiding this comment

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

A fun thing you can do in JS: if the key and the variable share the same name you can do:

return {
  title,
  author,
  publishedYear,
  genre
};

}

// DO NOT CHANGE THE LINE OF CODE BELOW (you can use it for testing your code)
Expand All @@ -50,8 +54,7 @@ const book = createBook(
*/
function printBookTitleAndYear(book: Book): string {
// write your code here...

return ""; // replace empty string with what you see is fit
return `${book.title} ${book["publishedYear"]}`; // replace empty string with what you see is fit
}

/**
Expand All @@ -66,7 +69,7 @@ function printBookTitleAndYear(book: Book): string {
*/
function addPageCount(book: Book, pageCount: number): Book {
// write your code here...

book.pageCount = pageCount;
return book;
}

Expand All @@ -88,7 +91,7 @@ function addPageCount(book: Book, pageCount: number): Book {
*/
function addISBN(book: Book, ISBN: string): Book {
// write your code here...

book.ISBN = ISBN;
return book;
}

Expand All @@ -110,7 +113,7 @@ function addISBN(book: Book, ISBN: string): Book {
*/
function updatePublishedYear(book: Book, newYear: number): Book {
// write your code here...

book.publishedYear = newYear;
return book;
}

Expand All @@ -135,8 +138,12 @@ function updatePublishedYear(book: Book, newYear: number): Book {
*/
function addSecondAuthor(book: Book, additionalAuthor: string): Book {
// write your code here...

return book;
return {
...book,
author: Array.isArray(book.author)
? [...book.author, additionalAuthor]
: [book.author, additionalAuthor],
};
}

export {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/challenge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("🌶️🌶️🌶️ Challenge", () => {
const { reviewer, comment } = newReview;
const updatedBook = addReview(book, reviewer, comment);
expect(updatedBook.reviews).toContainEqual(newReview);
expect(updatedBook.reviews.length).toBe(2);
expect(updatedBook?.reviews?.length).toBe(2);
});

it("should create a review array if one didn't already exist", () => {
Expand Down