Skip to content

Instantly share code, notes, and snippets.

@redrambles
Last active March 9, 2022 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redrambles/ae272705cc2f135756048a5090965a0d to your computer and use it in GitHub Desktop.
Save redrambles/ae272705cc2f135756048a5090965a0d to your computer and use it in GitHub Desktop.
class HashTable {
constructor(size) {
this.size = size;
this.table = new Array(this.size);
}
//lame-ass hash
hashIt(key) {
let hash = 0;
for (const char of key.toString()){
hash += key.charCodeAt(char)
}
// Grab an index between 0 and size of table - 1
return hash % this.size;
};
put(key, value) {
const index = this.hashIt(key);
this.table[index] = {[key]: value}
}
get(key) {
const index = this.hashIt(key);
if (this.table[index]){
return this.table[index][key]
} else {
// ain't nothing there, friend
return -1
}
}
remove(key) {
const index = this.hashIt(key);
if (this.table[index]){
this.table[index] = {}
} else {
console.log(`The table doesn't currently contain key: ${key}.`)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment