app.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. enum Category {
  2. Biography, // 0
  3. Poetry, // 1
  4. Fiction, // 2
  5. }
  6. let books: ({ author: string; available: boolean; category: Category; id: number; title: string })[] = [];
  7. function GetAllBooks() {
  8. if (books.length < 4) {
  9. books = [
  10. {
  11. author: 'James Joyce',
  12. available: true,
  13. category: Category.Fiction,
  14. id: 1,
  15. title: 'Ulysses',
  16. },
  17. {
  18. author: 'Ernest Hemingway',
  19. available: false,
  20. category: Category.Fiction,
  21. id: 2,
  22. title: 'A farewall to arms',
  23. },
  24. {
  25. author: 'Maya Angelou',
  26. available: true,
  27. category: Category.Poetry,
  28. id: 3,
  29. title: 'I know why the caged bird sings',
  30. },
  31. {
  32. author: 'Herman Melville',
  33. available: true,
  34. category: Category.Fiction,
  35. id: 4,
  36. title: 'Moby Dick',
  37. }
  38. ];
  39. }
  40. return books;
  41. }
  42. function LogFirstAvailable(books = GetAllBooks()): void {
  43. const numberOfBooks: number = books.length;
  44. let firstAvailable: string = '';
  45. for (let currentBook of books) {
  46. if (currentBook.available) {
  47. firstAvailable = currentBook.title;
  48. break;
  49. }
  50. }
  51. console.log(`Total number of books: ${numberOfBooks}.`);
  52. console.log(`First available: ${firstAvailable}.`);
  53. }
  54. function CheckoutBooks(customer: string, ...bookIds: number[]): string[] {
  55. console.log(`Checking out books for ${customer}.`);
  56. let booksCheckedOut: string[] = [];
  57. for (let id of bookIds) {
  58. let book = GetBookById(id);
  59. if (book.available) {
  60. booksCheckedOut.push(book.title);
  61. }
  62. }
  63. return booksCheckedOut;
  64. }
  65. function CreateCustomer(name: string, age?: number, city?: string) {
  66. console.log(`Name: ${name}.`);
  67. if (age) {
  68. console.log(`Age: ${age}.`);
  69. }
  70. if (city) {
  71. console.log(`City: ${city}.`);
  72. }
  73. }
  74. function CreateCustomerId(name: string, id: number): string {
  75. return name + id;
  76. }
  77. function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): Array<string> {
  78. console.log(`Getting books in category: ${Category[categoryFilter]}.`);
  79. const allBooks = GetAllBooks();
  80. const filteredTitles: string[] = [];
  81. for (let currentBook of allBooks) {
  82. if (currentBook.category === categoryFilter) {
  83. filteredTitles.push(currentBook.title);
  84. }
  85. }
  86. return filteredTitles;
  87. }
  88. function GetBookById(id: number) {
  89. const allBooks = GetAllBooks();
  90. return allBooks.filter(book => book.id === id)[0];
  91. }
  92. function LogBookTitles(titles: string[]): void {
  93. for (let title of titles) {
  94. console.log(title);
  95. }
  96. }
  97. //******************************************************************************
  98. function defaultParamsDemo() {
  99. CreateCustomer('Michelle');
  100. CreateCustomer('Leigh', 6);
  101. CreateCustomer('Marie', 12, 'Atlanta');
  102. }
  103. function functionTypeDemos() {
  104. let idGenerator: (chars: string, nums: number) => string;
  105. idGenerator = CreateCustomerId;
  106. let myID: string = idGenerator('Daniel', 10);
  107. console.log(myID);
  108. idGenerator = (name, id) => id + name;
  109. myID = idGenerator('Daniel', 10);
  110. console.log(myID);
  111. }
  112. // const fictionBookTitles = GetBookTitlesByCategory(Category.Fiction);
  113. // fictionBookTitles.forEach((val, idx, arr) => console.log(`${++idx} ${val}.`));
  114. // let poetryBookTitles = GetBookTitlesByCategory();
  115. // console.log(poetryBookTitles);
  116. function functionCallDefaultParamsDemo() {
  117. console.log("Book count:", books.length);
  118. books.unshift({
  119. author: "Anon1",
  120. available: true,
  121. category: Category.Biography,
  122. id: 42,
  123. title: "Added1",
  124. });
  125. console.log("Book count:", books.length);
  126. LogFirstAvailable();
  127. books.unshift({
  128. author: "Anon2",
  129. available: true,
  130. category: Category.Biography,
  131. id: 42,
  132. title: "Added2",
  133. });
  134. console.log("Book count:", books.length);
  135. // Default is now 5: it is evaluated on each call.
  136. LogFirstAvailable();
  137. }
  138. function restParamsDemo() {
  139. let myBooks = CheckoutBooks('Ripper', 1, 2, 3, 4);
  140. myBooks.forEach(title => console.log(title);
  141. }
  142. // defaultParamsDemo();
  143. // functionTypeDemos();
  144. // functionCallDefaultParamsDemo();
  145. restParamsDemo();