diff --git a/src/objects.ts b/src/objects.ts index ef6298f..2690f35 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -26,9 +26,13 @@ function createBook( publishedYear: number, genre: string ): Book { - // write your code here... - - return {} as Book; // replace "{} as Book" with what you see is fit + let book: Book = { + title: title, + author: author, + publishedYear: publishedYear, + genre: genre, + }; + return book; } // DO NOT CHANGE THE LINE OF CODE BELOW (you can use it for testing your code) @@ -49,9 +53,7 @@ const book = createBook( * // => "Hitchhiker's Guide to The Galaxy 1965" */ function printBookTitleAndYear(book: Book): string { - // write your code here... - - return ""; // replace empty string with what you see is fit + return book.title + " " + book["publishedYear"]; } /** @@ -65,7 +67,7 @@ function printBookTitleAndYear(book: Book): string { * // => { title: "Hitchhiker's Guide to The Galaxy", author: "Douglas Adams", publishedYear: 1965, genre: "Sci-Fi", pageCount: 320 } */ function addPageCount(book: Book, pageCount: number): Book { - // write your code here... + book.pageCount = pageCount; return book; } @@ -87,7 +89,7 @@ function addPageCount(book: Book, pageCount: number): Book { * // } */ function addISBN(book: Book, ISBN: string): Book { - // write your code here... + book.ISBN = ISBN; return book; } @@ -109,7 +111,7 @@ function addISBN(book: Book, ISBN: string): Book { * // } */ function updatePublishedYear(book: Book, newYear: number): Book { - // write your code here... + book.publishedYear = newYear; return book; } @@ -134,10 +136,18 @@ function updatePublishedYear(book: Book, newYear: number): Book { * // } */ function addSecondAuthor(book: Book, additionalAuthor: string): Book { - // write your code here... - + if (Array.isArray(book.author)) { + let newArray = book.author; + book.author.push(additionalAuthor); + book.author = newArray; + } else if (typeof book.author === "string") { + let newArray = [book.author]; + newArray.push(additionalAuthor); + book.author = newArray; + } return book; } +console.log(addSecondAuthor(book, "test")); export { createBook,