Return
- TypeScript course
- Beginner
- Lesson 84/89
- 5 min
Write the book return logic.
The returnBook function
returnBook returns a borrowed book. You find it with find; if the book exists and !available (is borrowed), you set it to true and return true.
function returnBook(lib: Library, bookId: string): boolean {
const book = lib.books.find(b => b.id === bookId);
if (book && !book.available) {
book.available = true;
return true;
}
return false;
}The return check
When returning, the book must be borrowed; that is, available must be false. We check this with the ! operator.
// Check when returning:
if (book && !book.available) {
book.available = true;
}Practice Write the returnBook function
Define Book, Member, Library interfaces. Write addBook and borrowBook functions. Write a function returnBook(lib: Library, bookId: string): boolean: find the book with find, if it exists and !available, set it to true and return true. Add a book, borrow it, try to return it twice, and print the results.
Starter code
// Book, Member, Library arayüzlerini tanımla
// addBook ve borrowBook fonksiyonlarını yaz
// returnBook(lib: Library, bookId: string): boolean yaz
// kitabı bul, ödünç verilmişse available true yap ve true döndür
// bir kitap ekle, ödünç al, iki kez iade etmeyi dene ve yazdırSample solution
interface Book { id: string; title: string; author: string; available: boolean; }
interface Member { id: string; name: string; }
interface Library { books: Book[]; }
function addBook(lib: Library, book: Book): void {
lib.books.push(book);
}
function borrowBook(lib: Library, bookId: string): boolean {
const book = lib.books.find(b => b.id === bookId);
if (book && book.available) {
book.available = false;
return true;
}
return false;
}
function returnBook(lib: Library, bookId: string): boolean {
const book = lib.books.find(b => b.id === bookId);
if (book && !book.available) {
book.available = true;
return true;
}
return false;
}
const lib: Library = { books: [] };
addBook(lib, { id: "b1", title: "TypeScript", author: "Ada", available: true });
borrowBook(lib, "b1");
console.log(returnBook(lib, "b1"));
console.log(returnBook(lib, "b1"));Expected output
true false
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When does returnBook return true?
- When the book cannot be found
- When the book is already on the shelf
- When the book is borrowed Correct answer
- Always
Explanation: returnBook returns true when the book is found and available is false (borrowed). If the book is already on the shelf or cannot be found, it returns false.
2 Complete the available value
book.available = ____;
book.available = true;
Explanation: The available field of a returned book is set to true; the book is back on the shelf. false means borrowed; "true" is a string and 1 is a number, both wrong type.
3 Returning parts
| Term | Meaning |
|---|---|
returnBook |
Return a book |
!book.available |
Borrowed check |
book.available = true |
Put back on shelf |
return true |
Return successful |