-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhash-maps.js
43 lines (32 loc) · 985 Bytes
/
hash-maps.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
const names = ['benjamin', 'maya', 'reuben'];
///////////////////////////////////
// Making a hash map using an array
///////////////////////////////////
const hashMapArray = [];
// returns the length of the str plus the number of 'e' characters
function hashFunction(str) {
let eCount = 0;
for (let char of str) {
if (char.toLowerCase() === 'e') {
eCount++;
}
}
return eCount + str.length;
}
names.forEach(name => {
let hashKey = hashFunction(name);
hashMapArray[hashKey] = true;
});
console.log(hashMapArray);
// [ <4 empty items>, true, <3 empty items>, true, true ]
let hashKey = hashFunction('benjamin');
console.log(hashMapArray[hashKey]);
///////////////////////////////////
// Making a hash map using an object
///////////////////////////////////
const hashMapObject = {};
names.forEach(name => {
hashMapObject[name] = true;
});
console.log(hashMapObject);
// { "benjamin": true, "maya": true, "reuben": true }