function hashFn(string, limit = 7) {
const PRIME = 31;
let hashCode = 0;
for (let item of string) {
hashCode = PRIME * hashCode + item.charCodeAt();
}
return hashCode % limit;
}
function HashTable() {
this.storage = [];
this.limit = 7;
this.count = 0;
HashTable.prototype.put = function(key,value) {
let index = hashFn(key,this.limit);
let bucket = this.storage[index];
if(bucket == null) {
bucket = [];
this.storage[index] = bucket;
}
for(let item of bucket ) {
if(item[0] == key) {
item[1] = value
return
}
}
bucket.push([key,value]);
this.count++;
}
HashTable.prototype.get = function(key) {
let index = hashFn(key,this.limit);
let bucket = this.storage[index];
if(!bucket) {
return null;
}
for(let tuple of bucket) {
if(tuple[0] == key) {
return tuple[1]
}
}
return null;
}
HashTable.prototype.resize = function(newLimit) {
const oldStorage = this.storage;
this.storage = [];
this.limit = 0;
this.count = 0;
for(let bucket of oldStorage) {
if(bucket === null) {
continue;
}
for(tuple of bucket) {
this.put(tuple[0],tuple[1])
}
}
}
HashTable.prototype.isPrime = function isPrime(num) {
for(let i = 2; i < parseInt(Math.sqrt(num)); i++) {
if(num % i) {
return false
}
}
return true
}
}
console.log(isPrime(1))
console