Function HashTable() {this.stroage = []; // Hash array this.count = 0; // The number of elements this.limit = 7; / / the length of the array / / method / / hash function HashTable. Prototype. HashFunc = function (STR, size) {/ / 1. Define the hashCode variable let hashCode = 0; // Get unicode encoding for each character (let I = 0; i < str.length; i++) { hashCode = 37 * hashCode + str.charCodeAt(i); } // 3 let index = hashCode % size return index; Prototype. Put = function (key,value) {let index = this.hashfunc (key,this.limit); let bucket = this.stroage[index]; if(! bucket) { bucket = []; this.stroage[index] = bucket; } let tuple = []; for (let i = 0; i < bucket.length; i++) { tuple = bucket[i]; if(tuple[0] == key) { tuple[1] = value; return; } } bucket.push([key,value]) this.count += 1; If (this.count * 0.75 > this.limit) {this.resize(this.limit * 2)} index = this.hashFunc(key,this.limit); let bucket = this.stroage[index]; if (! bucket) { return null; } let tuple = []; for (let i = 0; i < bucket.length; i++) { tuple = bucket[i]; if(tuple[0] == key) { return tuple[1]; } } return null; } / / delete HashTable. Prototype. Remove = function (key) {let index = this. HashFunc (key, this limit). let bucket = this.stroage[index]; if(! bucket) return null; let tuple = []; for (let i = 0; i < bucket.length; i++) { tuple = bucket[i]; if(tuple[0] == key) { bucket.splice(i,1); this.count -= 1; If (this.count * 0.25 < this.limit) {this.resize(this.limit / 2); } return; } } return null; } / / expansion HashTable. Prototype. The resize = function (newLimit) {let oldStroage = this. Stroage. this.stroage = []; this.count = 0; while (! this.isPrime(newLimit)) { newLimit++ } this.limit = newLimit; let bucket = []; for (let i = 0; i < oldStroage.length; i++) { bucket = oldStroage[i]; if(! bucket) continue; let tuple = []; for (let j = 0; j < bucket.length; j++) { tuple = bucket[j]; this.put(tuple[0],tuple[1]); }}} / / it is a prime HashTable. Prototype. IsPrime = function (num) {for (let I = 2; i < Math.sqrt(num); i++) { if( num % i == 0) { return false; } } return true; } } let hashTable = new HashTable();Copy the code