1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- enum Category {
- Biography, // 0
- Poetry, // 1
- Fiction, // 2
- }
- function GetAllBooks() {
- let books = [
- {
- author: 'James Joyce',
- available: true,
- category: Category.Fiction,
- title: 'Ulysses',
- },
- {
- author: 'Ernest Hemingway',
- available: false,
- category: Category.Fiction,
- title: 'A farewall to arms',
- },
- {
- author: 'Maya Angelou',
- available: true,
- category: Category.Poetry,
- title: 'I know why the caged bird sings',
- },
- {
- author: 'Herman Melville',
- available: true,
- category: Category.Fiction,
- title: 'Moby Dick',
- }
- ];
- return books;
- }
- function LogFirstAvailable(books): void {
- const numberOfBooks: number = books.length;
- let firstAvailable: string = '';
- for (let currentBook of books) {
- if (currentBook.available) {
- firstAvailable = currentBook.title;
- break;
- }
- }
- console.log(`Total number of books: ${numberOfBooks}.`);
- console.log(`First available: ${firstAvailable}.`);
- }
- function GetBookTitlesByCategory(categoryFilter: Category): Array<string> {
- console.log(`Getting books in category: ${Category[categoryFilter]}.`);
- const allBooks = GetAllBooks();
- const filteredTitles: string[] = [];
- for (let currentBook of allBooks) {
- if (currentBook.category === categoryFilter) {
- filteredTitles.push(currentBook.title);
- }
- }
- return filteredTitles;
- }
- function LogBookTitles(titles: string[]): void {
- for (let title of titles) {
- console.log(title);
- }
- }
- const poetryBooks = GetBookTitlesByCategory(Category.Poetry);
- LogBookTitles(poetryBooks);
|