Please help on challenge "create a hash table"

I cannot see where my error is.

I’m trying to solve Coding Interview Prep > Data Structures> Create a Hash Table

My code is as follow:

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 = function(key, value) {
    let h = hash(key);
    if (this.collection.hasOwnProperty(h)) {
      let prop = [key, value];
      this.collection[h].push(prop);
    } else {
      this.collection[h] = [];
      this.collection[h].push([key, value]);
    }
  }

  this.remove = function(key) {
    let h = hash(key);
    if (this.collection.hasOwnProperty(h)) {
      let value = this.collection[h];
      for (let i = 0; i < value.length; i++) {
        if (value[i][0] == key) {
          value.splice(i, 1);
          this.collection[h] = value;
          break;
        }
      }
      if (value.length == 0) {
        delete this.collection[h];
      }
    }
  }

  this.lookup = function(key) {
    let result = this.collection[hash(key)];
    if (result != undefined) {
      for (let elem of result) {
        if (elem[0] == key) {
          return elem[1];
        }
      }
    }
    return null;
  }
  // Only change code above this line
};

// tests
var hs = new HashTable();
console.log(hs);
console.log("");

hs.add("alfa", "ventuno");
console.log("add alfa:\n", hs.collection);
console.log("");

hs.add("beta", "ventidue");
console.log("add beta:\n", hs.collection);
console.log("");

hs.add("gamma", "ventitre");
console.log("add gamma:\n", hs.collection);
console.log("");

hs.add("ammag", "venticinque");
console.log("add ammag (collision):\n", hs.collection);
console.log("");

hs.add("ateb", "ventiquattro");
console.log("add ateb (collision):\n", hs.collection);
console.log("");

console.log("log beta:", hs.lookup("beta"))
console.log("log ateb:", hs.lookup("ateb"))
console.log("log gamma:", hs.lookup("gamma"))
console.log("log ammag:", hs.lookup("ammag"))
console.log("");

hs.remove("beta");
console.log("remove beta:\n", hs.collection);
console.log("");

hs.remove("ammag");
console.log("remove ammag:\n", hs.collection);
console.log("");

console.log("log beta:", hs.lookup("beta"))
console.log("log ateb:", hs.lookup("ateb"))
console.log("log gamma:", hs.lookup("gamma"))
console.log("log ammag:", hs.lookup("ammag"))
console.log("");

As usual, I added some tests to visualize the code behavior, and all the tests give the results that one expects.

But the sistem test return the following error:

The remove method should accept a key as input and
should remove the associated key value pair.

Well, it seems to me that the remove method actually accept a key and remove the associated key-value pair, as you can see following the tests.

Can you please give me a hint to find my error?

I haven’t looked too deep into this challenge in terms of trying to understand it, but here’s my thought:

If you remove this: if (value.length == 0) and simply keep the delete statement regardless of length, the challenge works.

Additionally: If I am not mistaken, the entire for loop isn’t needed, since the challenge only requires you to delete the key value pair, if a valid key is provided. (In any case the for loop isn’t needed to pass the challenge)
delete this.collection[h] will successfully do this.

You’re right, by deleting that line the challenge passes. Thank you.

But it seems to me that my solution is the correct one, and maybe the system tests need to be recalibrated.

With the current solution, the remove method does not delete a single key-value pair, but all the key-value pairs associated with a certain hash. And for each hash there can be many key-value pairs, as many as there are collisions.

One way to handle collisions is to just store both
key-value pairs at that index. Then, upon lookup
of either, you would have to iterate through the
bucket of items to find the key you are looking for.

My solution only removes a single key-value pair, and only removes a hash if it is empty, i.e. if it no longer contains any key-value pairs. And it seems to me that this is exactly how a hash table should behave.

The second should remove a key-value pair when
passed a key.

(If you copy-paste my code with all the tests into the freeCodeCamp editor you can see that the collisions are handled correctly.)