Hash Table

by jessekinsman

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.2/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.css">
<link rel="stylesheet" href="https://codepen.io/btholt/pen/WrwzJZ.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.js"></script>
<div id='target'>no snapshots</div>

Babel + JSX

/*

  A HashTableSet!
  
  Name your class/newable-function HashTableSet.
  
  With a set, you want to put in a value to check later if it's in the collection.
  You are going to watch a sufficiently large array to assure you don't have collisions. I did 255
  to start with. When added, use a hashing function to hash the string and put in your table.
  The class should have three functions:
  
  add -   function - takes a string as an input, hashes it, and puts in its table
  check - function - takes a string and returns true if it exists in its table; otherwise returns false
  hash -  function - takes a string and a max number and return a number between 0 and the max number
                     function must be idempotent; the same string and max number will always yield the
                     same output        
*/

class HashTableSet {
	constructor() {
  	this.table = new Array(255);
  }
  add(value) {
  	this.table[this.hash(value, 255)] = value;
  }
  check(value) {
  	if (this.table[this.hash(value, 255)]) {
    	return true;
    } else {
    	return false;
    }
  }
  hash(input, max) {
  let num = 0;
  	for (let i = 0; i< input.length; i++) {
    	num += input.charCodeAt(i) * i;
    }
    return num % max;
  }
}




// unit tests
// do not modify the below code
describe('hash table set', function() {
  it('hash', () => {
    const table = new HashTableSet();
    expect(table.hash('test 1', 50)).toEqual(table.hash('test 1', 50));
    expect(table.hash('test 2', 10)).toEqual(table.hash('test 2', 10));
    expect(table.hash('a much longer strings than the other ones', 255)).toEqual(table.hash('a much longer strings than the other ones', 255));
    expect(table.hash('1 tset', 50)).not.toEqual(table.hash('test 1', 50));
    expect(table.hash('a much longer strings than the other ones', 2)).toBeLessThan(3);
  });
  it('add and check', () => {
    const table = new HashTableSet();
    table.add('hi');
    table.add('this is fun');
    table.add('another...