Complete the code to create an empty hash table object in JavaScript.
const hashTable = [1];In JavaScript, an empty hash table can be created using an empty object literal {}.
Complete the code to add a key-value pair to the hash table.
hashTable[[1]] = 'apple';
Keys in JavaScript objects must be strings or symbols. Here, the key is the string 'fruit'.
Fix the error in the hash function to return a valid index within the array size.
function hash(key, size) {
let total = 0;
for (let char of key) {
total += char.charCodeAt(0);
}
return total [1] size;
}The modulo operator % ensures the hash value fits within the array size by returning the remainder.
Fill both blanks to complete the function that checks if a key exists in the hash table.
function hasKey(hashTable, key) {
return [1] in hashTable && hashTable[[2]] !== undefined;
}We check if the key exists in the hashTable and its value is not undefined.
Fill all three blanks to complete the function that deletes a key from the hash table if it exists.
function deleteKey(hashTable, key) {
if ([1] in [2]) {
[3] hashTable[key];
return true;
}
return false;
}The function checks if the key exists in the hashTable, then uses the delete operator to remove it.
