From a32b4c2ab24341fbcd80ff01402729031d7d0c2f Mon Sep 17 00:00:00 2001 From: HAlGhanim Date: Sun, 9 Mar 2025 03:17:20 +0300 Subject: [PATCH 1/4] Done with the task but the challenge test needs safe call / optional chaining in line 33 to work --- src/challenge.ts | 4 +++- src/objects.ts | 25 ++++++++++++++++--------- src/tests/challenge.test.ts | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/challenge.ts b/src/challenge.ts index d697960..470ce34 100644 --- a/src/challenge.ts +++ b/src/challenge.ts @@ -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 }]); return book; } diff --git a/src/objects.ts b/src/objects.ts index ef6298f..e4539c6 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -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 } // DO NOT CHANGE THE LINE OF CODE BELOW (you can use it for testing your code) @@ -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 } /** @@ -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; } @@ -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; } @@ -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; } @@ -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 { diff --git a/src/tests/challenge.test.ts b/src/tests/challenge.test.ts index f85c5c5..b5b9b0a 100644 --- a/src/tests/challenge.test.ts +++ b/src/tests/challenge.test.ts @@ -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", () => { From b28b80f7f7ffaeaa8523ebd1357e9b7de6fe4b77 Mon Sep 17 00:00:00 2001 From: HAlGhanim Date: Sun, 9 Mar 2025 03:24:04 +0300 Subject: [PATCH 2/4] Changed the line 33 back to what it was originally so it can be fixed from your side --- src/tests/challenge.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/challenge.test.ts b/src/tests/challenge.test.ts index b5b9b0a..f85c5c5 100644 --- a/src/tests/challenge.test.ts +++ b/src/tests/challenge.test.ts @@ -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", () => { From 51dc191a918469817f60532d2494ab777174b8e5 Mon Sep 17 00:00:00 2001 From: HAlGhanim Date: Sun, 9 Mar 2025 12:12:41 +0300 Subject: [PATCH 3/4] Added safe call to challenge test --- src/tests/challenge.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/challenge.test.ts b/src/tests/challenge.test.ts index f85c5c5..b5b9b0a 100644 --- a/src/tests/challenge.test.ts +++ b/src/tests/challenge.test.ts @@ -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", () => { From 1d60dedad4f4d5df795c347b22fffed7a9b6e2ff Mon Sep 17 00:00:00 2001 From: HAlGhanim Date: Sun, 9 Mar 2025 15:30:11 +0300 Subject: [PATCH 4/4] Changed solution for addSecondAuthor to fit new test --- src/objects.ts | 20 +++++++++---------- src/tests/challenge.test.ts | 2 +- src/tests/objects.test.ts | 40 +++++++++++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/objects.ts b/src/objects.ts index e4539c6..7adf18e 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -28,10 +28,10 @@ function createBook( ): Book { // write your code here... return { - title: title, - author: author, - publishedYear: publishedYear, - genre: genre, + title, + author, + publishedYear, + genre, }; // replace "{} as Book" with what you see is fit } @@ -138,12 +138,12 @@ function updatePublishedYear(book: Book, newYear: number): Book { */ function addSecondAuthor(book: Book, additionalAuthor: string): Book { // write your code here... - return { - ...book, - author: Array.isArray(book.author) - ? [...book.author, additionalAuthor] - : [book.author, additionalAuthor], - }; + if (Array.isArray(book.author)) { + book.author.push(additionalAuthor); + } else { + book.author = [book.author, additionalAuthor]; + } + return book; } export { diff --git a/src/tests/challenge.test.ts b/src/tests/challenge.test.ts index b5b9b0a..a29c71b 100644 --- a/src/tests/challenge.test.ts +++ b/src/tests/challenge.test.ts @@ -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", () => { diff --git a/src/tests/objects.test.ts b/src/tests/objects.test.ts index f5a5ea0..bb498a7 100644 --- a/src/tests/objects.test.ts +++ b/src/tests/objects.test.ts @@ -55,34 +55,66 @@ describe("Books", () => { describe("Modifying Objects", () => { describe("addPageCount", () => { it("should add a pageCount property to the book", () => { + const bookCopy = { ...book }; const pageCount = faker.number.int({ min: 100, max: 2000 }); - const updatedBook = addPageCount(book, pageCount); + const updatedBook = addPageCount(bookCopy, pageCount); expect(updatedBook.pageCount).toBe(pageCount); }); + + it("should modify and return the original book object, NOT a copy", () => { + const bookCopy = { ...book }; + const pageCount = faker.number.int({ min: 100, max: 2000 }); + const updatedBook = addPageCount(bookCopy, pageCount); + expect(updatedBook).toBe(bookCopy); + }); }); describe("addISBN", () => { it("should add an ISBN to the book", () => { + const bookCopy = { ...book }; const isbn = faker.commerce.isbn(); - const updatedBook = addISBN(book, isbn); + const updatedBook = addISBN(bookCopy, isbn); expect(updatedBook.ISBN).toBe(isbn); }); + + it("should modify and return the original book object, NOT a copy", () => { + const bookCopy = { ...book }; + const isbn = faker.commerce.isbn(); + const updatedBook = addISBN(bookCopy, isbn); + expect(updatedBook).toBe(bookCopy); + }); }); describe("updatePublishedYear", () => { it("should update the published year of the book", () => { + const bookCopy = { ...book }; const publishYear = faker.date.past({ years: 100 }).getFullYear(); - const updatedBook = updatePublishedYear(book, publishYear); + const updatedBook = updatePublishedYear(bookCopy, publishYear); expect(updatedBook.publishedYear).toBe(publishYear); }); + + it("should modify and return the original book object, NOT a copy", () => { + const bookCopy = { ...book }; + const publishYear = faker.date.past({ years: 100 }).getFullYear(); + const updatedBook = updatePublishedYear(bookCopy, publishYear); + expect(updatedBook).toBe(bookCopy); + }); }); describe("addSecondAuthor", () => { it("should modify the author property to include an additional author", () => { + const bookCopy = { ...book }; const secondAuthor = faker.person.fullName(); - const updatedBook = addSecondAuthor({ ...book }, secondAuthor); + const updatedBook = addSecondAuthor(bookCopy, secondAuthor); expect(updatedBook.author).toEqual([book.author, secondAuthor]); }); + + it("should modify and return the original book object, NOT a copy", () => { + const bookCopy = { ...book }; + const secondAuthor = faker.person.fullName(); + const updatedBook = addSecondAuthor(bookCopy, secondAuthor); + expect(updatedBook).toBe(bookCopy); + }); }); }); });