Hi,
I got stack again… sorry.
Fails: Items are added using the hash function.
Hope I understand well this challenge and have to use hash() on key.
My code:
let called = 0;
const hash = string => {
called++;
let hashed = 0;
for (let i = 0; i < string.length; i++) {
hashed += string.charCodeAt(i);
}
return hashed;
};
const HashTable = function() {
this.collection = {};
// change code below this line
this.add = function(key, value) {
this.collection.hasOwnProperty(hash(key))
? this.collection[hash(key)][key] = value
: this.collection[hash(key)] = {
[key]: value
};
};
this.remove = function(key) {
if (key && this.collection.hasOwnProperty(hash(key))) delete this.collection[hash(key)][key];
};
this.lookup = function(key) {
if (key && this.collection.hasOwnProperty(hash(key))) return this.collection[hash(key)][key] || null;
return null;
}
// change code above this line
};
const myTable = new HashTable;
myTable.add("name", "csaba");
myTable.add("anme", "robin");
myTable.remove("anme");
console.log(myTable.lookup("anme"));
console.log(myTable);