Skip to content

Instantly share code, notes, and snippets.

@egermano
Created September 11, 2012 15:09
Show Gist options
  • Save egermano/3699524 to your computer and use it in GitHub Desktop.
Save egermano/3699524 to your computer and use it in GitHub Desktop.
Hash Tables
function HashTable(size) {
this.size = size;
this.buckets = new Array(size);
this.add = function(value) {
var index = this.hash(value);
this.buckets[index] = value;
};
this.hash = function(value) {
var sum = 0;
for (var i = 0; i < value.length; ++i) {
sum += value[i].charCodeAt() - 97;
}
return sum % this.size;
};
}
var hash = new HashTable(3);
hash.add('Germano');
hash.add('Bruno');
hash.add('Andrade');
console.log(hash);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment