Data Structures - Create a Hash Table

Tell us what’s happening:
I wrote my code using the method of Linear Probing, but it cannot pass the test. Could anyone give me a hand?

Your code so far

class ValuePair{
  constructor(key, value){
    this.key = key;
    this.value = value;
  }
  toString(){
    return `[#${this.key}: ${this.value}]`
  }
}

class HashTable {
  constructor(toStrFn = Object.prototype.toString){
    this.toStrFn = toStrFn;
    this.table = {};
  }
  loseloseHashCode(key){
    if(typeof key === "number"){
      return key;
    }
    const tableKey = this.toStrFn(key);
    let hash = 0;
    for(let i = 0; i < tableKey.length; i++){
      hash += tableKey.charCodeAt(i);
    }
    return hash % 37;
  }

  hashCode(key){
    return this.loseloseHashCode(key);
  }
  add(key, value){
    if(key != null && value != null){
      const position = this.hashCode(key);
      if(this.table[position] == null){
        this.table[position] = new ValuePair(key, value);
      }else{
        let index = position + 1;
        while(this.table[index] != null){
          index++;
        }
        this.table[index] = new ValuePair(key, value);
      }
      return true;
    }
    return false
  }

  lookup(key){
    const position = this.hashCode(key);
    if(this.table[position] != null){
      if(this.table[position].key === key){
        return this.table[position].value;
      }
      let index = position + 1;
      while(this.table[index] != null && this.table[index].key !== key){
        index++;
      }
      if(this.table[index] != null && this.table[index].key === key){
        return this.table[position].value;
      }
    }
    return undefined;
  }

  remove(key){
    const position = this.hashCode(key);
    if(this.table[position] != null){
      if(this.table[position].key === key){
        delete this.table[position];
        this.verifyRemoveSideEffect(key, position);
        return true;
      }
      let index = position + 1;
      while(this.table[index] != null && this.table[index].key !== key){
        index++;
      }
      if(this.table[index] != null && this.table[index].key === key){
        delete this.table[index];
        this.verifyRemoveSideEffect(key, index);
        return true;
      }
    }
    return false
  }

  verifyRemoveSideEffect(key, removedPosition){
    const hash = this.hashCode(key);
    let index = removedPosition + 1;
    while(this.table[index] != null){
      const posHash = this.hashCode(this.table[index].key);
      if(posHash <= hash || posHash <= removedPosition){
        this.table[removedPosition] = this.table[index];
        delete this.table[index];
        removedPosition = index;
      }
      index++;
    }
  }
}


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15

Challenge: Data Structures - Create a Hash Table

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.