hash_imp

Run Settings
LanguageJavaScript
Language Version
Run Command
// Forgot to handle collision case class HashTable { constructor(size){ this.data = new Array(size); } set(k,v) { let address = this._hash(k); if(!this.data[address]){ this.data[address] = []; //Also forgot to push and directly assigned [k,v] } this.data[address].push([k,v]); console.log(this.data); } get(k){ let address = this._hash(k); console.log(this.data[address][1]); } _hash(key) { let hash = 0; for (let i =0; i < key.length; i++){ hash = (hash + key.charCodeAt(i) * i) % this.data.length } return hash; } } const myHashTable = new HashTable(50); myHashTable.set('grapes', 10000) myHashTable.get('grapes') myHashTable.set('apples', 9) myHashTable.get('apples')
Editor Settings
Theme
Key bindings
Full width
Lines