There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Hello I have an issue with the second exercice of the Learn Hash Maps: JavaScript lesson.
We have to get the sum of the charcodes in a variable named hashCode.
I tried this :
hash(key) {
let hashCode = 0;
for (let i = 0; i < key.length; i++) {
hashCode += key.charCodeAt(i);
}
return hashCode;
}
But it returns the error : Incorrect:Did you add the hashCode and the character code to hashCode?
Which I think I did.
I also tried :
hashCode = hashCode + key.charCodeAt(i);
Which does not work either.
Apparently the only accepted answer is :
hashCode += hashCode + key.charCodeAt(i);
From my understanding, it adds twice the hashCode to hashCode. Which isn’t was is in the instructions.
So logically the code below should work as it does the same thing :
Hey all, I’d love feedback on how to make this exercise easier to understand. Adding the sum of hashCode and the character code to the hashCode again creates a deterministic and also non-reversible implementation of a hashing function. This avoids generating a duplicate index if keys have the same characters in different orders, such as bat and tab.
The reason we are adding in hashcode to itself with every character is so that the key is “directional” in that noodle will create a different hash than eldoon (see the example below). This is important so that the hash is unique to the given key.
For this example lets use two words cat and tac and pretend that these letters have very easy character codes.
c = 1
a = 2
t = 3
For cat this means that for each letter we add it, to the sum.
So as you can see, we have the same three letters, but very different end hashcodes.
If we only added in the code a single time, we would end up with a total of 6 for both words 0 + 1, 1 + 2, 3 + 3 = 6 vs 0 + 3, 3 + 2, 5 + 1 = 6 (please ignore the simple formatting).
Anyways I hope this helps, if there needs to be some further clarification, I’ll try to come back to this post.
I think, as a learner, who already had a first contact with Hash Table through CS50, that while learning Data Structures the hashing function in itself is pretty irrelevant and a “complicated” hashing function adds just information noise and nothing more. I read this FAQ cause probably also for other learners it was not totally clear why hashCode was summed two times. After having read the answer of catower is now clear to me. Me personally I would have inserted to this point a simpler and more understandable hashing algorithm.
Edit: Answering directly to @catower question, who ask for feedback. Probably inserting the explanation he wrote here would make this exercise easier to understand.