Add code for chapter 5 in ts (#104)
This commit is contained in:
11
05_hash_tables/ts/01_price_of_groceries.ts
Normal file
11
05_hash_tables/ts/01_price_of_groceries.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
interface HashTable<T> {
|
||||
[key: string]: T;
|
||||
}
|
||||
|
||||
const book: HashTable<number> = {};
|
||||
|
||||
book['apple'] = 0.67;
|
||||
book['milk'] = 1.49;
|
||||
book['avocado'] = 1.49;
|
||||
|
||||
console.log(book); // { apple: 0.67, milk: 1.49, avocado: 1.49 }
|
||||
18
05_hash_tables/ts/02_check_voter.ts
Normal file
18
05_hash_tables/ts/02_check_voter.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
interface HashTable<T> {
|
||||
[key: string]: T;
|
||||
}
|
||||
|
||||
const voted: HashTable<boolean> = {};
|
||||
|
||||
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!
|
||||
Reference in New Issue
Block a user