-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathhashtable.js
More file actions
67 lines (54 loc) · 1.53 KB
/
hashtable.js
File metadata and controls
67 lines (54 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class HashTable {
constructor(size = 7) {
this.map = Array(size);
}
_hash(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash = (hash + key.charCodeAt(i) * 23) % this.map.length;
}
return hash;
}
printHashTable() {
for (let i = 0; i < this.map.length; i++) {
console.log(`index ${i}:`, this.map[i]);
}
}
set(key,value) {
let index = this._hash(key);
if(!this.map[index]) this.map[index] = [];
this.map[index].push([key, value]);
return this;
}
get(key) {
let index = this._hash(key);
if(this.map[index]) {
for(let i = 0; i < this.map[index].length; i++) {
if(this.map[index][i][0] === key) {
return this.map[index][i][1];
}
}
}
return undefined;
}
keys() {
let keys = []
for(let i = 0; i < this.map.length; i++) {
if(this.map[i]) {
for(let j = 0; j < this.map[i].length; j++) {
keys.push(this.map[i][j][0]);
}
}
}
return keys;
}
}
let myHashTable = new HashTable();
myHashTable.set("bolts", 70);
myHashTable.set("nails", 120);
myHashTable.set("paint", 40);
myHashTable.set("tile", 60);
myHashTable.set("lumber", 90);
console.log(myHashTable.get("nails"));
console.log(myHashTable.keys());
myHashTable.printHashTable();