April 2018
Beginner to intermediate
426 pages
10h 19m
English
Now that we have added our elements, let's implement the get function so that we can retrieve their values, as follows:
get(key) { const position = this.hashCode(key); if (this.table[position] != null) { // {1} if (this.table[position].key === key) { // {2} return this.table[position].value; // {3} } let index = position + 1; // {4} while (this.table[index] != null && this.table[index].key !== key) { // {5} index++; } if (this.table[index] != null && this.table[index].key === key) { // {6} return this.table[position].value; // {7} } } return undefined; // {8}}
To retrieve a key's value, we first need to verify whether the key exists ({1}). If it does not exist, it means that the value is not in the hash table, so we can return ...