I’m working on the Learn Data Structures \ Create a Hash table, and I’m getting all the checkmarks except “The hash table should handle collisions.” … what exactly is that supposed to look like? The directions say only that "One way to handle collisions is to just store both key-value pairs at that index. Then, upon lookup of either, you would have to iterate through the bucket of items to find the key you are looking for. " It looks like array is not what they’re looking for…
var called = 0;
var hash = string => {
called++;
var hashed = 0;
for (var i = 0; i < string.length; i++) {
hashed += string.charCodeAt(i);
}
return hashed;
};
var HashTable = function() {
this.collection = {};
// change code below this line
this.add = (key, value) => {
let k = hash(key)
// console.log(key)
if (this.collection[k]==undefined) {
this.collection[k] = value
// console.log(this.collection)
} else {
console.log("!")
if (typeof this.collection[k] !== "object") {
this.collection[k] = [(key, this.collection[k]), (key, value)]
} else {
console.log("~")
this.collection[k] = [...this.collection[k], (key, value)]
}
console.log(this.collection)
}
return key
}
this.remove = (key) => {
let k = hash(key)
delete this.collection[k]
}
this.lookup = (key) => {
let k = hash(key);
if (this.collection.hasOwnProperty(k)){
//console.log(this.collection[k])
return this.collection[k]
} else {
return null
}
}
// change code above this line
};