无法从 hashMap 读取未定义的属性“长度”

问题描述

我正在尝试使用 hashmap 和获取来构建我的哈希函数

Cannot read property 'length' of undefined

这是我的代码

class HashMap{
         table = new Array(17);
    
    
    get(key){
        let idx = hashfun(key,this.table.length)
        return this.table[idx]
    }

    set(key,value){
        let idx = hashfun(key,this.table.length)
        return this.table[idx]
    }
}

function hashfun(s,arrlen){
    console.log("***",s)
    let hash = 17;
    for(let i=0; i< s.length; i++){
        hash = (13 * hash * s.charCodeAt(i)) % arrlen;
    }
    return hash
}

let ht = new HashMap();
ht.set("trying")
ht.get()

我正在尝试将键转换为 Int 并将键分配给索引。如果我做错了什么,请告诉我。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)