Complete the code to create an empty hash table using a JavaScript object.
const hashTable = [1];In JavaScript/TypeScript, an empty object {} is commonly used to represent a hash table.
Complete the hash function to compute the index by taking the modulus of the key with the table size.
function hash(key: number, size: number): number {
return key [1] size;
}The modulus operator % returns the remainder and is used to keep the index within the table size.
Fix the error in the hash function to correctly handle string keys by summing character codes before modulus.
function hash(key: string, size: number): number {
let total = 0;
for (let i = 0; i < key.length; i++) {
total += key.charCodeAt(i);
}
return total [1] size;
}The modulus operator % ensures the hash value fits within the table size.
Fill both blanks to create a hash function that sums character codes and returns the modulus with size.
function hash(key: string, size: number): number {
let sum = 0;
for (let i = 0; i < key.length; i++) {
sum [1]= key.charCodeAt(i);
}
return sum [2] size;
}The operator += adds each character code to sum, and % keeps the result within the table size.
Fill both blanks to create a hash function that converts a string key to a number index within the table size.
function hash(key: string, size: number): number {
let hashValue = 0;
for (let [1] = 0; [1] < key.length; [1]++) {
hashValue += key.charCodeAt([1]);
}
return hashValue [2] size;
}Use i as the loop variable, % for modulus, and i to access characters.
