Complete the code to create a simple key-value store using a hash map.
const map = new Map<string, number>(); map.set('apple', [1]); console.log(map.get('apple'));
The value stored for the key 'apple' should be a number, here 5.
Complete the code to check if a key exists in the hash map.
if (map.[1]('apple')) { console.log('Key exists'); }
The correct method to check if a key exists in a Map is has.
Fix the error in the code to retrieve a value safely from the hash map.
const value = map.get('banana'); if (value [1] undefined) { console.log('Value:', value); } else { console.log('No value found'); }
Use strict inequality !== to check that value is not undefined.
Fill both blanks to create a hash map from an array of keys with initial value 0.
const keys = ['a', 'b', 'c']; const map = new Map<string, number>(keys.map(key => [key, [1]])); console.log(map.get([2]));
Each key maps to 0 initially, and we check the value for key 'a'.
Fill all three blanks to update a hash map value by incrementing it safely.
const count = map.get('x') [1] 0; map.set('x', count [2] 1); console.log(map.get('x') === [3]);
Use || to default to 0 if undefined, then add 1, and check if result is 1.
