I am failing this test case
The remove method accepts a key as input and removes the associated key value pair.
As best I can tell from debugger I have removed the key value pair. I’ve tried this so many ways - maybe I just need a second set of eyes to see what I’m missing.
I have tried removing the entire nested object from this.collection and also tried leaving the (empty) object in place just removing key value pair.
Any help appreciated!
Your code so far
var called = 0;
var hash = (string) => {
called++;
var hash = 0;
for (var i = 0; i < string.length; i++) { hash += string.charCodeAt(i); }
return hash;
};
var HashTable = function () {
this.collection = {};
// change code below this line
this.add = function (k, v) {
const curHash = hash(k);
if (!this.collection[curHash]) {
this.collection[curHash] = {};
}
this.collection[curHash][k] = v;
}
this.remove = function (key) {
const curHash = hash(key);
console.log(this.collection)
if (this.collection[curHash][key]) {
delete this.collection[curHash][key];
if (Object.entries(this.collection[curHash]).length === 0) {
delete this.collection[curHash];
}
}
console.log(this.collection)
/*
if(this.lookup(key)){
console.log(this.lookup(key))
delete this.collection[curHash]
}
*/
}
this.lookup = function (key) {
const curHash = hash(key);
if (this.collection.hasOwnProperty(curHash) && this.collection[curHash].hasOwnProperty(key)) {
return this.collection[curHash][key];
}
return
}
// change code above this line
};
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-hash-table