123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- enum Category {
- Biography, // 0
- Poetry, // 1
- Fiction, // 2
- }
- function GetAllBooks() {
- let books = [
- {
- author: 'James Joyce',
- available: true,
- category: Category.Fiction,
- id: 1,
- title: 'Ulysses',
- },
- {
- author: 'Ernest Hemingway',
- available: false,
- category: Category.Fiction,
- id: 2,
- title: 'A farewall to arms',
- },
- {
- author: 'Maya Angelou',
- available: true,
- category: Category.Poetry,
- id: 3,
- title: 'I know why the caged bird sings',
- },
- {
- author: 'Herman Melville',
- available: true,
- category: Category.Fiction,
- id: 4,
- 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 CreateCustomerId(name: string, id: number): string {
- return name + id;
- }
- 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 GetBookById(id: number) {
- const allBooks = GetAllBooks();
- return allBooks.filter(book => book.id === id)[0];
- }
- function LogBookTitles(titles: string[]): void {
- for (let title of titles) {
- console.log(title);
- }
- }
- //******************************************************************************
- let x: number;
- let idGenerator: (chars: string, nums: number) => string;
- idGenerator = CreateCustomerId;
- let myID: string = idGenerator('Daniel', 10);
- console.log(myID);
- idGenerator = (name, id) => id + name;
- myID = idGenerator('Daniel', 10);
- console.log(myID);
- // const fictionBookTitles = GetBookTitlesByCategory(Category.Fiction);
- // fictionBookTitles.forEach((val, idx, arr) => console.log(`${++idx} ${val}.`));
|