-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.js
94 lines (82 loc) · 2.91 KB
/
hash.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var called = 0;
var hash = string => {
// example of the string: 'cat'
called++;
var hashed = 0;
for (var i = 0; i < string.length; i++) {
hashed += string.charCodeAt(i);
// example at each iteration if string is cat:
// what is string.charCodeAt(i); if i =0 and string is cat then string.charCodeAt(i) = 99
/// what is the char code of 'c'?: 99
// what is the char code of 'a'?: 97
// what is the char code of 't'?: 116
// what is the sum of the char codes of 'cat': (99 + 97 + 116) = 314
// what if it was 'dog':
// what is the char code of 'd': 100
// what is the char code of 'o': 111
// what is the char code of 'g': 103
// what if it was 'bunny':
// what is the char code of 'b': 98
// what is the char code of 'u': 117
// what is the char code of 'n': 110
// what is the char code of 'n': 110
// what is the char code of 'y': 121
// what is the sum of the char codes of 'bunny': (98 + 117 + 110 + 110+ 121) = 594
}
return hashed;
};
// how does hash tables work?
// hash tables are a data structure that stores data in a hash table
var HashTable = function() {
this.collection = {};
// Only change code below this line
//add should accept a key value pair to add to the hash table
this.add = (key,value) => {
const hashedKey = hash(key); // example: hash('cat') => would be 314 // what happens if differnt letters add up to the same number? : Then
this.collection[hashedKey] = this.collection[hashedKey] || {}; // example: this.collection[1234] = {};
this.collection[hashedKey][key]=value; //example: this.collection[1234]['cat'] = 'meow';
// example of what this collection looks like:
// this.collection[314] = {
// 'cat': 'meow'
// }
// what if we add another key value pair to the hash table?
// this.collection[594] ={
// 'bunny': 'thump'
// }
// example of what this collection looks like after adding another key value pair:
// this.collection[314] = {
// 'cat': 'meow'
// }
// this.collection[594] ={
// 'bunny': 'thump'
// }
// what if we add another key value pair to the hash table?
// this.collection[314] = {
// 'dog': 'woof'
// }
// now with cat dog and bunny what does this collection look like?
// this.collection[314] = {
// 'cat': 'meow',
// 'dog': 'woof'
// }
// this.collection[594] ={
// 'bunny': 'thump'
// }
// how would i call the dog?
// this.collection[314]['dog']
// this.collection[594]['bunny']
// this.collection[314]['cat']
}
}
this.remove = (key) => {
const hashedKey = hash(key);
delete this.collection[hashedKey][key]
}
this.lookup = (key) => {
const haskedKey = hash(key);
return this.collection[hashedKey]
const hashedKey = hash(key);
return this.collection[hashedKey][key];
}
// Only change code above this line
};