Having trouble with Create a Hash Table challenge

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);

I moved your question to a new topic. Please create new topics when asking questions about your code related to a challenge.

Thank you.

Looks like there is a bug in the tests. I have created a GitHub issue to propose a possible solution.

@lendoo You can avoid this bug (and pass all the tests) by only calling the has function a single time during your code. Currently you are calling it within the hasOwnProperty method and within your ternary expression. Only calling it once would increase the efficiency of your code and make it DRY.

Thank you,
I saved the hash() method in a constant. The code now pass the test.
const currentKey = hash(key);