为什么要用%来计算hashCode索引转换?

Why is % used to calculate hashCode index conversion?

假设我的哈希桶计数为 100 Table。我的哈希码对于密钥 A 为 500,对于密钥 B 为 600。这些都解析为 0 对于 hashCode % this.bucketCount,这是不同哈希码的冲突。

我想知道为什么要用%来计算指数。有人可以解释这方面的数学吗?为什么那个数学输出的索引是我应该插入节点的地方?

HashTable.prototype.hashFunction = function(key){
    var hash = 0;
    for (var i=0;i< key.length; i++){
        console.log(key.charCodeAt(i))
        hash += key.charCodeAt(i)
    }
    return hash;
};

HashTable.prototype.convertHashToIndex = function(hashCode) {
    return hashCode % this.bucketCount;
};

因为有bucketCount个桶,所以用除以桶数的模数(或余数)来保证结果适合可用的桶.如果你不取模,你会得到无法存储的结果。