Skip to content

HashMap: Modify paragraph to ensure clarity #29908

@goran1010

Description

@goran1010

Checks

Describe your suggestion

Following up on this issue a learner raised in TOP Discord where multiple people have agreed that the following paragraph is both confusing and ambiguous, I propose the following changes to address it. Change:

However, there is one edge case our hash function still needs to address. For very long keys, our hash code will exceed the maximum integer value allowed by JavaScript. Once that happens, calculations become inaccurate, and the chance of collisions significantly increases. One way to avoid this issue is to apply the modulo % operator on each iteration instead of outside the loop at the end. This ensures the output never becomes larger than our bucket’s length.

To:

However, there is one edge case our hash function still needs to address. For very long keys, our hash code will exceed the maximum integer value allowed by JavaScript. Once that happens, calculations become inaccurate, and the chance of collisions significantly increases. One way to avoid this issue is to apply the modulo % operator on each iteration instead of outside the loop at the end.

You may remember from the previous lesson that we used modulo % operator at the end of the hash function to fit the final hash into the bucket array. This approach still works for sizing, but placing modulo % operator inside the loop helps avoid internal overflow for long keys while still keeping the result within bucket range.

Where the sentence "This ensures the output never becomes larger than our bucket’s length." is removed since it's not technically accurate, because moving the modulo operator from outside the loop to inside the loop solves a different problem - modulo operator outside the loop already ensures that output never becomes larger than our bucket’s length, moving it inside solves the problem of inaccurate calculations in case of long key strings.

Second paragraph is added to explain what exactly does "apply the modulo % operator on each iteration instead of outside the loop at the end" refer to and where, since this was also a point of confusion.
It also correctly explains why we move the operator inside as opposed to leaving it outside the loop.

Path

Node / JS

Lesson Url

https://www.theodinproject.com/lessons/javascript-hashmap

(Optional) Discord Name

notevenbatman

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions