Solutions
Solution 1 (Click to Show/Hide)
let called = 0;
let hash = string => {
called++;
var hashed = 0;
for (var i = 0; i < string.length; i++) {
hashed += string.charCodeAt(i);
}
return hashed;
};
let HashTable = function() {
this.collection = {};
// Only change code below this line
this.add = (key, value) => {
const hashedKey = hash(key);
this.collection[hashedKey] = this.collection[hashedKey] || {};
this.collection[hashedKey][key] = value;
}
this.lookup = (key) => {
const hashedKey = hash(key);
return this.collection[hashedKey][key];
}
this.remove = (key) => {
const hashedKey = hash(key);
delete this.collection[hashedKey][key];
if (Object.keys(this.collection[hashedKey]).length == 0)
delete this.collection[hashedKey];
}
// Only change code above this line
};