Again… It would be much, much more fruitful to debug your code than try to understand somebody else’s solution. I’d be happy to help you get your code working. Writing code and reading code are two different skills, and practicing reading doesn’t help you write code.
Your first question doesn’t really make sense. Collision means that two keys have the same hash. If you reset the value associated with a key, that is not a collision.
I’m not sure what ‘simply store the value’ means to you. That line creates this.collection[hashedKey] an an empty object if it doesn’t exist.
!! coerces to a boolean value. It will have the same effect in an if statement, but the author of this code wanted to explicitly coerce to a boolean value instead of implicitly.
In a hash table you take a key of any length and create a hash of a reduced length. It’s easier to index into the lookup table with the smaller hash value.
Real hashes rarely collide (map two different keys to the same hash), but you still have to consider what to do if you have a collision.
So, the hard part of this challenge is figuring out a way to store multiple keys with the same hash and return the right associated value.
So you have a few issues going on with making that work.
This gets whatever is stored at collection[key]… But here
Here you are trying to update the value instead of the collection.
Also, you still have broken the connection between the original key and the value.
So… The key to this challenge is storing both the key and the value in a sensible way associated with collection[hash]
I guess what’s flying above my head are the tools to store multiple values at the same index. Looking at this as an array w/ a hash function & modulus arithmetic to map to indices, I don’t understand how one index (even in the solution in question, originally), can store multiple values. There’s the concept of using linked list at every index but that’s not how the solution accomplishes this. What is the interpreter doing when … :
add( …
this.collection[hashedKey][key] = value; // when it reads this line and collision is afoot?
I’ve taken the time to study hash tables in greater detail. I’ve come to the real dilemma I face with digesting the info. If we take a key to generate an index yet store both they key and value, how many times can we use the ‘’ operator? Is the answer thrice?
Is the following example, extending from the same FCC hashtable challenge, runnable code:
this.collection[index][key][value]
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 = {};
// Only change code below this line
this.add = function (keyValue, value) {
const indexKey = hash(keyValue);
console.log("the hash for", keyValue, "is", indexKey);
this.collection[indexKey] = value;
}
this.remove = function (keyValue) {
const indexKey = hash(keyValue);
console.log("removing all data associated with hash", indexKey);
delete this.collection[indexKey];
}
this.lookup = function (keyValue) {
const indexKey = hash(keyValue);
return this.collection[indexKey];
}
// Only change code above this line
};
let hashTable = new HashTable;
console.log("adding value for key 'pats'");
hashTable.add("pats", 2);
console.log("value added: ", hashTable.lookup("pats"));
console.log("\nadding value for the key 'spat'");
hashTable.add("spat", 42);
console.log("value: ", hashTable.lookup("spat"));
console.log("\nchecking value for the key 'pats'");
console.log("value: ", hashTable.lookup("pats"));
I see a mess but I finally get the solution. We need to first instantiate an object, in place /at index (i.e. ‘{}’ notation) per addition, to hold each key-value pair added. Nice.