Skip to content

Commit f01fe09

Browse files
committed
chapter 07: [Dictionaries and Hashes]
1 parent 5ec1287 commit f01fe09

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

examples/chapter07/01-Dictionaries.js

+6
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ console.log(dictionary.values()); // ["gandalf@email.com", "tyrion@email.com"]
2020

2121
console.log(dictionary.keyValues()); // [{key: "Gandalf", value: "gandalf@email.com"}, {key: "Tyrion", value: "tyrion@email.com"}]
2222
console.log(dictionary.toString()); // [#Gandalf: gandalf@email.com],[#Tyrion: tyrion@email.com]
23+
24+
dictionary.forEach((k, v) => {
25+
console.log('forEach: ', `key: ${k}, value: ${v}`);
26+
});
27+
// forEach: key: Gandalf, value: gandalf@email.com
28+
// forEach: key: Tyrion, value: tyrion@email.com

examples/chapter07/02-HashTable.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,34 @@ const { HashTable } = PacktDataStructuresAlgorithms;
22

33
const hash = new HashTable();
44

5-
hash.put('Gandalf', 'gandalf@email.com');
6-
hash.put('John', 'johnsnow@email.com');
7-
hash.put('Tyrion', 'tyrion@email.com');
8-
hash.put('Aaron', 'aaron@email.com');
9-
hash.put('Donnie', 'donnie@email.com');
10-
hash.put('Ana', 'ana@email.com');
5+
console.log(hash.hashCode('Ygritte') + ' - Ygritte');
6+
console.log(hash.hashCode('Jonathan') + ' - Jonathan');
7+
console.log(hash.hashCode('Jamie') + ' - Jamie');
8+
console.log(hash.hashCode('Jack') + ' - Jack');
9+
console.log(hash.hashCode('Jasmine') + ' - Jasmine');
10+
console.log(hash.hashCode('Jake') + ' - Jake');
11+
console.log(hash.hashCode('Nathan') + ' - Nathan');
12+
console.log(hash.hashCode('Athelstan') + ' - Athelstan');
13+
console.log(hash.hashCode('Sue') + ' - Sue');
14+
console.log(hash.hashCode('Aethelwulf') + ' - Aethelwulf');
15+
console.log(hash.hashCode('Sargeras') + ' - Sargeras');
16+
17+
hash.put('Ygritte', 'ygritte@email.com');
1118
hash.put('Jonathan', 'jonathan@email.com');
1219
hash.put('Jamie', 'jamie@email.com');
13-
hash.put('Sue', 'sue@email.com');
14-
hash.put('Mindy', 'mindy@email.com');
15-
hash.put('Paul', 'paul@email.com');
20+
hash.put('Jack', 'jack@email.com');
21+
hash.put('Jasmine', 'jasmine@email.com');
22+
hash.put('Jake', 'jake@email.com');
1623
hash.put('Nathan', 'nathan@email.com');
24+
hash.put('Athelstan', 'athelstan@email.com');
25+
hash.put('Sue', 'sue@email.com');
26+
hash.put('Aethelwulf', 'aethelwulf@email.com');
27+
hash.put('Sargeras', 'sargeras@email.com');
1728

1829
console.log('**** Printing Hash **** ');
1930

2031
console.log(hash.toString());
21-
// {5 => [#Sue: sue@email.com]},{10 => [#Nathan: nathan@email.com]},{13 => [#Ana: ana@email.com]},{16 => [#Aaron: aaron@email.com]},{19 => [#Gandalf: gandalf@email.com]},{29 => [#John: johnsnow@email.com]},{32 => [#Paul: paul@email.com]}
32+
// {4 => [#Ygritte: ygritte@email.com]},{5 => [#Aethelwulf: aethelwulf@email.com]},{7 => [#Athelstan: athelstan@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Sargeras: sargeras@email.com]}
2233

2334
console.log('**** Get **** ');
2435

src/js/data-structures/hash-table.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ export default class HashTable {
6868
const keys = Object.keys(this.table);
6969
let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;
7070
for (let i = 1; i < keys.length; i++) {
71-
objString = `${objString},{${keys[i]} => ${this.table[
72-
keys[i]
73-
].toString()}}`;
71+
objString = `${objString},{${keys[i]} => ${this.table[keys[i]].toString()}}`;
7472
}
7573
return objString;
7674
}

0 commit comments

Comments
 (0)