FAQ: Learn Hash Maps: JavaScript - Hashing

This community-built FAQ covers the “Hashing” exercise from the lesson “Learn Hash Maps: JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Pass the Technical Interview with JavaScript

FAQs on the exercise Hashing

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 (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 (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 (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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 :

hashCode = hashCode + hashCode + key.charCodeAt(i);

But it does not work weirdly enough.

Am I missing something or is this exercice flawed ?

Thanks.

2 Likes

The hint makes no sense…

The index of the current character is saved in the variable i .

variable += count + string.charCodeAt(i);

Where the heck did count come from? @lilybird, anybody on curriculum who can perhaps clear the air here?

1 Like

Can do! I’ll check with the team and then circle back here.

Edit: This has been fixed.

2 Likes

I have the same issue. I think there could be a problem in the test file:

expect(hashed, `Does your \`.hash()\` return the correct value?`).to.be.equal(1678);

Since hashed was produced from the string ‘test’ , It should check for 448 as returned value, not 1678.

Hi @lilybird,

Are you by any chance able to ask the team to add more details to the following code. Is there a reason we are adding hashCode twice?

hashCode += hashCode + key.charCodeAt(i);

Having a hard time understanding why we are not doing this instead:

hashCode += key.charCodeAt(i);

Would greatly appreciate your help!

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.

Code = 0 #starting code
Code = 0 + 0 + 1 #code + code + c (sum 1)
Code = 1 + 1 + 2 # code + code + a (sum 4)
Code = 4 + 4 + 3 # code + code + t (sum 11)
Code = 11 # final code

Comparatively, tac would look like this instead:

Code = 0 #starting code
Code = 0 + 0 + 3 #code + code + t (sum 3)
Code = 3 + 3 + 2 # code + code + a (sum 8)
Code = 8 + 8 + 1 # code + code + c (sum 17)
Code = 17 # final code

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.

10 Likes

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.

2 Likes

I agree. @catower 's explanation is clearer and should be added.