Skip to content
Open

done #22

Changes from all 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
68 changes: 55 additions & 13 deletions src/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,24 @@ function createBook(
publishedYear: number,
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,
};
}

const newBook = createBook(
"JavaScript: The Definitive Guide",
"David Flanagan",
2020,
"Programming"
);
console.log(newBook);

// DO NOT CHANGE THE LINE OF CODE BELOW (you can use it for testing your code)
const book = createBook(
const originalBook = createBook(
"Hitchhiker's Guide to The Galaxy",
"Douglas Adams",
1965,
Expand All @@ -51,9 +62,11 @@ 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"]}`;
}

console.log(printBookTitleAndYear(originalBook));

/**
* `addPageCount` function:
* - Accepts:
Expand All @@ -67,8 +80,13 @@ function printBookTitleAndYear(book: Book): string {
function addPageCount(book: Book, pageCount: number): Book {
// write your code here...

return book;
return {
...book,
pageCount: pageCount,
};
}
const bookWithPages = addPageCount(originalBook, 320);
console.log(bookWithPages);

/**
* `addISBN` function:
Expand All @@ -83,15 +101,21 @@ function addPageCount(book: Book, pageCount: number): Book {
* // author: "Douglas Adams",
* // publishedYear: 1965,
* // genre: "Sci-Fi",
* // ISBN: "978-3-16-148410-0" // ✅ added ISBN
* // ISBN: "978-3-16-148410-0"
* // }
*/
function addISBN(book: Book, ISBN: string): Book {
// write your code here...

return book;
return {
...book,
ISBN: ISBN,
};
}

const bookWithISBN = addISBN(bookWithPages, "978-3-16-148410-0");
console.log(bookWithISBN);

/**
* `updatePublishedYear` function:
* - Accepts:
Expand All @@ -103,17 +127,23 @@ function addISBN(book: Book, ISBN: string): Book {
* // => {
* // title: "Hitchhiker's Guide to The Galaxy",
* // author: "Douglas Adams",
* // publishedYear: 2022, // ✅ updated publishedYear
* // publishedYear: 2022,
* // genre: "Sci-Fi",
* // ISBN: "978-3-16-148410-0"
* // }
*/
function updatePublishedYear(book: Book, newYear: number): Book {
// write your code here...

return book;
return {
...book,
publishedYear: newYear,
};
}

const bookWithUpdatedYear = updatePublishedYear(bookWithISBN, 2022);
console.log(bookWithUpdatedYear);

/**
* `addSecondAuthor` function:
* - Accepts:
Expand All @@ -127,18 +157,30 @@ function updatePublishedYear(book: Book, newYear: number): Book {
* addSecondAuthor(book, "John Doe");
* // => {
* // title: "Hitchhiker's Guide to The Galaxy",
* // author: ["Douglas Adams", "John Doe"], // ✅ two authors now
* // author: ["Douglas Adams", "John Doe"]
* // publishedYear: 1965,
* // genre: "Sci-Fi",
* // ISBN: "978-3-16-148410-0"
* // }
*/
function addSecondAuthor(book: Book, additionalAuthor: string): Book {
// write your code here...

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

const finalBook = addSecondAuthor(bookWithUpdatedYear, "John Doe");
console.log(finalBook);

export {
createBook,
printBookTitleAndYear,
Expand Down
Loading