Create a Hash Table failing one test

Create a Hash Table failing one test
0

#1

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


#2

The problem is not your remove method. The problem is your lookup method.

Look at the challenge instructions carefully for what to return in the lookup method.

The test suite is checking for a specific return value which you are not returning for the test related to checking your remove method.


#3

I was hoping you would chime in.

Duh! I saw it right away once you pointed me back to the lookup method. It is embarrassing how much time I spent not seeing that.

Thank you so much. Glad that is over with.


#4

My next response was going to be something like “You will kick yourself when you see what is missing in the last return statement.” Glad you figured it out on your own.