From 0bc042fc08c988bef6083994b2574675411b0d67 Mon Sep 17 00:00:00 2001 From: Abdullah Al-Rashidi Date: Tue, 22 Jul 2025 19:35:21 +0300 Subject: [PATCH 1/2] objects.ts done --- src/objects.ts | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/objects.ts b/src/objects.ts index ef6298f..9505d26 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -1,9 +1,9 @@ // don't change this interface interface Book { title: string; - author: string | string[]; + author?: string | string[]; publishedYear: number; - genre: string; + genre?: string; pageCount?: number; ISBN?: string; } @@ -27,8 +27,13 @@ function createBook( genre: string ): Book { // write your code here... - - return {} as Book; // replace "{} as Book" with what you see is fit + const firstBook: Book = { + title, + author, + publishedYear, + genre, + }; + return firstBook; // 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 +55,11 @@ const book = createBook( */ function printBookTitleAndYear(book: Book): string { // write your code here... - - return ""; // replace empty string with what you see is fit + // const theBook: Book = { + // title: "The Book", + // publishedYear: 1965, + // }; + return `${book.title}, ${book["publishedYear"]}`; // replace empty string with what you see is fit } /** @@ -66,7 +74,7 @@ function printBookTitleAndYear(book: Book): string { */ function addPageCount(book: Book, pageCount: number): Book { // write your code here... - + book.pageCount = pageCount; return book; } @@ -88,7 +96,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 +118,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,7 +143,13 @@ 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); + } else if (book.author) { + book.author = [book.author, additionalAuthor]; + } else { + book.author = [additionalAuthor]; + } return book; } From f47dd300880a2a4f4d2c5f4778be9003d544d591 Mon Sep 17 00:00:00 2001 From: Abdullah Al-Rashidi Date: Wed, 23 Jul 2025 19:55:38 +0300 Subject: [PATCH 2/2] IF statement changed --- src/objects.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/objects.ts b/src/objects.ts index 9505d26..dd25586 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -145,11 +145,12 @@ function addSecondAuthor(book: Book, additionalAuthor: string): Book { // write your code here... if (Array.isArray(book.author)) { book.author.push(additionalAuthor); - } else if (book.author) { - book.author = [book.author, additionalAuthor]; } else { - book.author = [additionalAuthor]; + book.author = book.author + ? [book.author, additionalAuthor] + : [additionalAuthor]; } + return book; }