Challenge: Hash Table - Remove Test Failing

Hey everyone!

I’m currently running into an issue with the hash table challenge, here I’ve implemented a 2D array with hashes that correspond to another array of array of values. For example, something with the hash of 392 might look like:

392:[[key,value],[key1,value1]]

I’ve passed all the tests except the remove tests, however when trying to test the code manually, I see that the output looks okay to me. I was wondering if there was something that I may have done wrong and if anybody could help me out!

The code, test, and output are seen below.

var called = 0;
var hash = string => {
  called++;
  var hashed = 0;
  for (var i = 0; i < string.length; i++) {
    hashed += string.charCodeAt(i);
  }
  return hashed;
};
var HashTable = function() {
  this.collection = {};
  // Only change code below this line
  this.add = (key,value) =>{
    if (this.collection[hash(key)]){
      this.collection[hash(key)].push([key,value])
    } else{
      this.collection[hash(key)] = [[key,value]]
    }
  }

  this.remove = (key)=>{
    if (this.collection[hash(key)]){
      for (let i =0; i<this.collection[hash(key)].length;i++){
        if (this.collection[hash(key)][i][0]===key){
          // splice fn
          this.collection[hash(key)].splice(i,1)
        }
      }
    }
  }

  this.lookup = (key)=>{
    for (let i=0; i<this.collection[hash(key)].length;i++){
      if (this.collection[hash(key)][i][0]===key){
        return this.collection[hash(key)][i][1]
      }
    }
  }
  // Only change code above this line
};

Test:


let test = new HashTable();

test.add('key', 'value');

test.add('yek', 'value');

test.add('altKey', 'value');

test.remove('yek');

console.log(test.lookup('key'));

console.log(test.lookup('yek'));

console.log(test.lookup('altKey'));

Output:

value
undefined
value

Link to the challenge:

Seems it is expected for remove method to remove the hashed key, once all keys from it were removed:

let test = new HashTable();
test.add('key', 'value');
test.remove('key');
console.log(test.collection);

This was the solution! Thank you very much!! Adding this code snippet to remove solved the issue.

   if (this.collection[hash(key)].length===0){
        delete this.collection[hash(key)]
}