From e3bdf568d8685199ccdb5f40c6da5066c2792cd0 Mon Sep 17 00:00:00 2001 From: Alex Voloshchenko Date: Tue, 19 Mar 2019 22:57:30 +0200 Subject: [PATCH] Add code for chapter 5 in ts --- 05_hash_tables/ts/01_price_of_groceries.ts | 11 +++++++++++ 05_hash_tables/ts/02_check_voter.ts | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 05_hash_tables/ts/01_price_of_groceries.ts create mode 100644 05_hash_tables/ts/02_check_voter.ts diff --git a/05_hash_tables/ts/01_price_of_groceries.ts b/05_hash_tables/ts/01_price_of_groceries.ts new file mode 100644 index 00000000..1c5228a6 --- /dev/null +++ b/05_hash_tables/ts/01_price_of_groceries.ts @@ -0,0 +1,11 @@ +interface HashTable { + [key: string]: T; +} + +const book: HashTable = {}; + +book['apple'] = 0.67; +book['milk'] = 1.49; +book['avocado'] = 1.49; + +console.log(book); // { apple: 0.67, milk: 1.49, avocado: 1.49 } \ No newline at end of file diff --git a/05_hash_tables/ts/02_check_voter.ts b/05_hash_tables/ts/02_check_voter.ts new file mode 100644 index 00000000..1369b3dd --- /dev/null +++ b/05_hash_tables/ts/02_check_voter.ts @@ -0,0 +1,18 @@ +interface HashTable { + [key: string]: T; +} + +const voted: HashTable = {}; + +function check_voter(name: string): void { + if (voted[name]) { + console.log('kick them out!'); + } else { + voted[name] = true; + console.log('let them vote!'); + } +} + +check_voter("tom"); // let them vote! +check_voter("mike"); // let them vote! +check_voter("mike"); // kick them out! \ No newline at end of file