app.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. id: 1,
  13. title: 'Ulysses',
  14. },
  15. {
  16. author: 'Ernest Hemingway',
  17. available: false,
  18. category: Category.Fiction,
  19. id: 2,
  20. title: 'A farewall to arms',
  21. },
  22. {
  23. author: 'Maya Angelou',
  24. available: true,
  25. category: Category.Poetry,
  26. id: 3,
  27. title: 'I know why the caged bird sings',
  28. },
  29. {
  30. author: 'Herman Melville',
  31. available: true,
  32. category: Category.Fiction,
  33. id: 4,
  34. title: 'Moby Dick',
  35. }
  36. ];
  37. return books;
  38. }
  39. function LogFirstAvailable(books): void {
  40. const numberOfBooks: number = books.length;
  41. let firstAvailable: string = '';
  42. for (let currentBook of books) {
  43. if (currentBook.available) {
  44. firstAvailable = currentBook.title;
  45. break;
  46. }
  47. }
  48. console.log(`Total number of books: ${numberOfBooks}.`);
  49. console.log(`First available: ${firstAvailable}.`);
  50. }
  51. function CreateCustomerId(name: string, id: number): string {
  52. return name + id;
  53. }
  54. function GetBookTitlesByCategory(categoryFilter: Category): Array<string> {
  55. console.log(`Getting books in category: ${Category[categoryFilter]}.`);
  56. const allBooks = GetAllBooks();
  57. const filteredTitles: string[] = [];
  58. for (let currentBook of allBooks) {
  59. if (currentBook.category === categoryFilter) {
  60. filteredTitles.push(currentBook.title);
  61. }
  62. }
  63. return filteredTitles;
  64. }
  65. function GetBookById(id: number) {
  66. const allBooks = GetAllBooks();
  67. return allBooks.filter(book => book.id === id)[0];
  68. }
  69. function LogBookTitles(titles: string[]): void {
  70. for (let title of titles) {
  71. console.log(title);
  72. }
  73. }
  74. //******************************************************************************
  75. let x: number;
  76. let idGenerator: (chars: string, nums: number) => string;
  77. idGenerator = CreateCustomerId;
  78. let myID: string = idGenerator('Daniel', 10);
  79. console.log(myID);
  80. idGenerator = (name, id) => id + name;
  81. myID = idGenerator('Daniel', 10);
  82. console.log(myID);
  83. // const fictionBookTitles = GetBookTitlesByCategory(Category.Fiction);
  84. // fictionBookTitles.forEach((val, idx, arr) => console.log(`${++idx} ${val}.`));