title: Custom Hashable implementation in Javascript
body: <p>I am fairly <s>stupid</s> new to programming. I want to implement a simple hashtable in javascript (for educational purposes). Everything works correctly, except when I try to overwrite some value it holds the previous value. For example when I try hashTable.set(cherry, 100), then hashTable.set(cherry, 4) and then hashTable.get(cherry) gives me 100 instead of 4. I've tried going over it with debugger, but turns out debugger doesn't help if you are fairly <del>stupid</del> new to programming.
  Heres the code:</p>
  <pre><code>    class HashTable {
    constructor(size) {
      this.data = new Array(size);
    }
  
    _hash(key) {
      let hash = 0;
      for (let i = 0; i &lt; key.length; i++) {
        hash = (hash + key.charCodeAt(i) * i) % this.data.length;
      }
      return hash;
    }
  
    set(key, value) {
      const address = this._hash(key);
      if (!this.data[address]) {
        this.data[address] = [];
      }
      if (Array.isArray(this.data[address])) {
        for (let arr of this.data[address].values()) {
          if (arr[0] === key) {
            const arr1 = arr[1];
            arr[1] === value;
            return;
          }
        }
      }
      this.data[address].push([key, value]);
    }
    get(key) {
      const address = this._hash(key);
      const currentNode = this.data[address];
      if (currentNode) {
        for (let arr of currentNode) {
          if (arr[0] === key) {
            return arr[1];
          }
        }
      }
      return undefined;
    }
  }
  
  const myHashTable = new HashTable(2);
  myHashTable.set(&quot;cherry&quot;, 100);
  myHashTable.set(&quot;cherry&quot;, 4);
  console.log(myHashTable.get(&quot;cherry&quot;)); // returns 100 instead of 4
  myHashTable.set(&quot;peach&quot;, 9);
  console.log(myHashTable.get(&quot;peach&quot;));
  myHashTable.set(&quot;apple&quot;, 2);
  console.log(myHashTable.get(&quot;apple&quot;));
  </code></pre>