function HashTable(size = 13) {
const _store = [];
const _size = size;
function hash(string) {
let index = 0;
for(let i = 0; i < string.length; i++) {
index += string.charCodeAt(i) * (i+1);
}
return index % _size;
}
function findMatchingIndex(list, key) {
for(let i = 0; i < list.length; i++) {
if(list[i][0] === key) return i;
}
}
return {
setElement(key, value) {
const index = hash(key);
if(!_store[index]) {
_store[index] = [
[key, value]
];
}
else {
const list = _store[index];
const matchingIndex = findMatchingIndex(list, key);
if(matchingIndex) {
list[matchingIndex] = [key, value];
return;
}
list.push([key, value]);
}
},
getElement(key) {
const index = hash(key);
if(_store[index]) {
const list = _store[index];
const matchingIndex = findMatchingIndex(list, key);
if(matchingIndex) return list[matchingIndex][1];
}
},
dump() {
return _store;
}
}
}
const ht = new HashTable();
ht.setElement("name", "Vasya");
ht.setElement("boroda", "Boroda");
ht.setElement("band", "Ramones");
ht.setElement("operating system", "Linux");
ht.setElement("soer", "soer");
ht.setElement("lost", "4815162342");
ht.setElement("male", "Alex");
ht.setElement("film", "Godfather");
ht.setElement("price", "100500");
console.log(ht.getElement("boroda"));
console.log(ht.getElement("male"));
console.log(ht.getElement("soer"));
console.log(ht.getElement("lost"));
console.log(ht.getElement("price"));
console.log(ht.getElement("film"));
console.log(ht.getElement("name"));
console.log(ht.getElement("band"));
console.log(ht.getElement("operating system"));
console.log(ht.getElement("8080"));
ht.dump();