I'm having trouble getting writing the getter method of my hash class based on what I have in the setter class.
The error I'm getting is: error: Uncaught TypeError: Cannot read property '1' of undefined
setItem = (key, value, value2) => {
const idx = HashStringToInt(key, this.table.length);
if (this.table[idx]) {
this.table.push([key,[value, value2]]);
} else {
this.table[idx] = [[key, [value, value2]]]
}
}
getItem = key => {
const idx = HashStringToInt(key, this.table.length);
if (!this.table[idx]) {
return null;
}
return this.table[idx].find(x => x[0] === key)[1]; //this doesn't work
}
this.table.lengthto your hash function and you keep changing the length, how are you expected to receive the same hashas before and after adding an item?HashStringToInt("foo", 1)andHashStringToint("foo", 2)will produce different results. You can remedy that by using a constant size of the array, so the hash identity of the same key is stable.ifstatement insetItemflipped around? If a given key exists, then a new item is pushed to the array, if it doesn't exist, then a key is overwritten.