Add code for chapter 5 in ts (#104)

This commit is contained in:
Alex
2019-03-28 23:51:19 +02:00
committed by Aditya Bhargava
parent 393b8e135d
commit 97003f16df
2 changed files with 29 additions and 0 deletions

View 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 }

View 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!