|
@@ -4,42 +4,46 @@ enum Category {
|
|
Fiction, // 2
|
|
Fiction, // 2
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+let books: ({ author: string; available: boolean; category: Category; id: number; title: string })[] = [];
|
|
|
|
+
|
|
function GetAllBooks() {
|
|
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',
|
|
|
|
- }
|
|
|
|
- ];
|
|
|
|
|
|
+ if (books.length < 4) {
|
|
|
|
+ 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;
|
|
return books;
|
|
}
|
|
}
|
|
|
|
|
|
-function LogFirstAvailable(books): void {
|
|
|
|
|
|
+function LogFirstAvailable(books = GetAllBooks()): void {
|
|
|
|
|
|
const numberOfBooks: number = books.length;
|
|
const numberOfBooks: number = books.length;
|
|
let firstAvailable: string = '';
|
|
let firstAvailable: string = '';
|
|
@@ -54,11 +58,35 @@ function LogFirstAvailable(books): void {
|
|
console.log(`First available: ${firstAvailable}.`);
|
|
console.log(`First available: ${firstAvailable}.`);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+function CheckoutBooks(customer: string, ...bookIds: number[]): string[] {
|
|
|
|
+ console.log(`Checking out books for ${customer}.`);
|
|
|
|
+
|
|
|
|
+ let booksCheckedOut: string[] = [];
|
|
|
|
+ for (let id of bookIds) {
|
|
|
|
+ let book = GetBookById(id);
|
|
|
|
+ if (book.available) {
|
|
|
|
+ booksCheckedOut.push(book.title);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return booksCheckedOut;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function CreateCustomer(name: string, age?: number, city?: string) {
|
|
|
|
+ console.log(`Name: ${name}.`);
|
|
|
|
+ if (age) {
|
|
|
|
+ console.log(`Age: ${age}.`);
|
|
|
|
+ }
|
|
|
|
+ if (city) {
|
|
|
|
+ console.log(`City: ${city}.`);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
function CreateCustomerId(name: string, id: number): string {
|
|
function CreateCustomerId(name: string, id: number): string {
|
|
return name + id;
|
|
return name + id;
|
|
}
|
|
}
|
|
|
|
|
|
-function GetBookTitlesByCategory(categoryFilter: Category): Array<string> {
|
|
|
|
|
|
+function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): Array<string> {
|
|
console.log(`Getting books in category: ${Category[categoryFilter]}.`);
|
|
console.log(`Getting books in category: ${Category[categoryFilter]}.`);
|
|
|
|
|
|
const allBooks = GetAllBooks();
|
|
const allBooks = GetAllBooks();
|
|
@@ -84,16 +112,59 @@ function LogBookTitles(titles: string[]): void {
|
|
}
|
|
}
|
|
|
|
|
|
//******************************************************************************
|
|
//******************************************************************************
|
|
-let x: number;
|
|
|
|
-let idGenerator: (chars: string, nums: number) => string;
|
|
|
|
-idGenerator = CreateCustomerId;
|
|
|
|
|
|
+function defaultParamsDemo() {
|
|
|
|
+ CreateCustomer('Michelle');
|
|
|
|
+ CreateCustomer('Leigh', 6);
|
|
|
|
+ CreateCustomer('Marie', 12, 'Atlanta');
|
|
|
|
+}
|
|
|
|
|
|
-let myID: string = idGenerator('Daniel', 10);
|
|
|
|
-console.log(myID);
|
|
|
|
|
|
+function functionTypeDemos() {
|
|
|
|
+ let idGenerator: (chars: string, nums: number) => string;
|
|
|
|
+ idGenerator = CreateCustomerId;
|
|
|
|
|
|
-idGenerator = (name, id) => id + name;
|
|
|
|
-myID = idGenerator('Daniel', 10);
|
|
|
|
-console.log(myID);
|
|
|
|
|
|
+ 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);
|
|
// const fictionBookTitles = GetBookTitlesByCategory(Category.Fiction);
|
|
// fictionBookTitles.forEach((val, idx, arr) => console.log(`${++idx} ${val}.`));
|
|
// fictionBookTitles.forEach((val, idx, arr) => console.log(`${++idx} ${val}.`));
|
|
|
|
+// let poetryBookTitles = GetBookTitlesByCategory();
|
|
|
|
+// console.log(poetryBookTitles);
|
|
|
|
+
|
|
|
|
+function functionCallDefaultParamsDemo() {
|
|
|
|
+ console.log("Book count:", books.length);
|
|
|
|
+ books.unshift({
|
|
|
|
+ author: "Anon1",
|
|
|
|
+ available: true,
|
|
|
|
+ category: Category.Biography,
|
|
|
|
+ id: 42,
|
|
|
|
+ title: "Added1",
|
|
|
|
+ });
|
|
|
|
+ console.log("Book count:", books.length);
|
|
|
|
+ LogFirstAvailable();
|
|
|
|
+ books.unshift({
|
|
|
|
+ author: "Anon2",
|
|
|
|
+ available: true,
|
|
|
|
+ category: Category.Biography,
|
|
|
|
+ id: 42,
|
|
|
|
+ title: "Added2",
|
|
|
|
+ });
|
|
|
|
+ console.log("Book count:", books.length);
|
|
|
|
+ // Default is now 5: it is evaluated on each call.
|
|
|
|
+ LogFirstAvailable();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function restParamsDemo() {
|
|
|
|
+ let myBooks = CheckoutBooks('Ripper', 1, 2, 3, 4);
|
|
|
|
+ myBooks.forEach(title => console.log(title);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// defaultParamsDemo();
|
|
|
|
+// functionTypeDemos();
|
|
|
|
+// functionCallDefaultParamsDemo();
|
|
|
|
+restParamsDemo();
|