title: Calling a Method on a Linked List inside of Javascript implementation of a Hash Table
body: <p>I'm working on creating a Hash Table in Javascript to better understand the data structure. I implemented it with a Linked List at each table position to deal with conflicts. </p>
  
  <p>The problem is that I cannot call .add on this.storage[index], as it is undefined. When I log this.storage[index] I see my head nodes in my linked list and the methods that I need. So I'm not sure what the problem is.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var Node = function(key, value) {
    this.key = key;
    this.value = value;
    this.next = null;
  };
  
  var LinkedList = function() {
    this.head = new Node('head');
    this.add = function(key, value) {
      var node = new Node(key, value);
      var curNode = this.head;
      while (curNode.next !== null) {
        curNode = curNode.next;
      }
      curNode.next = node;
    };
    this.find = function(key) {
      var curNode = this.head;
      while (curNode.key !== key &amp;&amp; curNode.next !== null) {
        curNode = curNode.next;
      }
      return curNode.value;
    };
  };
  
  var HashTable = function(max) {
    this.max = max;
    this.storage = [];
    for (var i = 0; i &lt; max; ++i) {
      this.storage[i] = new LinkedList();
    }
    this.hash = function(key) {
      var sum = 0;
      for (var i = 0; i &lt; key.length; ++i) {
        sum += key[i].charCodeAt() - 97;
      }
      return sum % this.max;
    };
    this.addValue = function(key, value) {
      var index = this.hash(key);
      this.storage[index].add(key, value);
    };
    this.getValue = function(key) {
      var index = this.hash(key);
      return this.storage[index].find(key);
    };
  };
  
  var hash = new HashTable(5);
  hash.addValue("I");
  hash.addValue("would");
  hash.addValue("like");
  hash.addValue("coffee");
  hash.getValue('I');</code></pre>
  </div>
  </div>
  </p>