From c158788c4f448ef910c791bbab1b1de20cac607a Mon Sep 17 00:00:00 2001 From: Sayuri Cero Date: Thu, 24 Jul 2025 19:26:41 +0300 Subject: [PATCH] Initial solution --- src/challenge.ts | 25 +++++++++++++++++++++++++ src/objects.ts | 23 +++++++++++++++-------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/challenge.ts b/src/challenge.ts index d697960..66bd9d8 100644 --- a/src/challenge.ts +++ b/src/challenge.ts @@ -39,8 +39,33 @@ function addReview( comment: string ): ReviewedBook { // write your code here... + if (!book.reviews) { + book.reviews = []; + } + + const newReview = { reviewer, comment }; + book.reviews.push(newReview); return book; } +const book: ReviewedBook = { + title: "1984", + author: "George Orwell", + publishedYear: 1949, + genre: "Dystopian", +}; + +const updatedBook = addReview(book, "Alice", "A thought-provoking novel!"); +console.log(updatedBook); + +function addReviewImmutable( + book: ReviewedBook, + reviewer: string, + comment: string +): ReviewedBook { + const newReview = { reviewer, comment }; + const reviews = book.reviews ? [...book.reviews, newReview] : [newReview]; + return { ...book, reviews }; +} export { addReview, Review, ReviewedBook }; diff --git a/src/objects.ts b/src/objects.ts index ef6298f..e2bd9cc 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -27,8 +27,11 @@ function createBook( genre: string ): Book { // write your code here... - - return {} as Book; // replace "{} as Book" with what you see is fit + title = "JavaScript: The Definitive Guide"; + author = "David Flanagan"; + publishedYear = 2020; + genre = "Programming"; + return { title, author, publishedYear, genre } as Book; // 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 +53,8 @@ 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; // 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 = 320; 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 = "978-3-16-148410-0"; 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 = 2022; return book; } @@ -135,7 +138,11 @@ function updatePublishedYear(book: Book, newYear: number): Book { */ function addSecondAuthor(book: Book, additionalAuthor: string): Book { // write your code here... - + if (Array.isArray(book.author)) { + book.author.push(additionalAuthor = "John Doe"); + } else { + book.author = [book.author, additionalAuthor = "John Doe"]; + } return book; }