app.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. enum Category {
  2. Biography, // 0
  3. Poetry, // 1
  4. Fiction, // 2
  5. }
  6. function GetAllBooks() {
  7. let books = [
  8. {
  9. author: 'James Joyce',
  10. available: true,
  11. category: Category.Fiction,
  12. title: 'Ulysses',
  13. },
  14. {
  15. author: 'Ernest Hemingway',
  16. available: false,
  17. category: Category.Fiction,
  18. title: 'A farewall to arms',
  19. },
  20. {
  21. author: 'Maya Angelou',
  22. available: true,
  23. category: Category.Poetry,
  24. title: 'I know why the caged bird sings',
  25. },
  26. {
  27. author: 'Herman Melville',
  28. available: true,
  29. category: Category.Fiction,
  30. title: 'Moby Dick',
  31. }
  32. ];
  33. return books;
  34. }
  35. function LogFirstAvailable(books): void {
  36. const numberOfBooks: number = books.length;
  37. let firstAvailable: string = '';
  38. for (let currentBook of books) {
  39. if (currentBook.available) {
  40. firstAvailable = currentBook.title;
  41. break;
  42. }
  43. }
  44. console.log(`Total number of books: ${numberOfBooks}.`);
  45. console.log(`First available: ${firstAvailable}.`);
  46. }
  47. function GetBookTitlesByCategory(categoryFilter: Category): Array<string> {
  48. console.log(`Getting books in category: ${Category[categoryFilter]}.`);
  49. const allBooks = GetAllBooks();
  50. const filteredTitles: string[] = [];
  51. for (let currentBook of allBooks) {
  52. if (currentBook.category === categoryFilter) {
  53. filteredTitles.push(currentBook.title);
  54. }
  55. }
  56. return filteredTitles;
  57. }
  58. function LogBookTitles(titles: string[]): void {
  59. for (let title of titles) {
  60. console.log(title);
  61. }
  62. }
  63. const poetryBooks = GetBookTitlesByCategory(Category.Poetry);
  64. LogBookTitles(poetryBooks);