We'll use the returned index of hashStr
to decide where the entered value should go in this._storage
.
A word on a collisions: collisions
are when a hash function returns the same index for more than one key and are out of the scope of this question. However, there are ways to handle such issues using additional data structures.
xxxxxxxxxx
70
"`var dict = new Hashmap(); dict.set('jess', '213-559-6840'); dict.set('james', '123-456-7890'); dict.get('james')` should return `'123-456-7890'`"
var assert = require('assert');
class Hashmap {
constructor() {
// implement this
}
set(key, val) {
// implement this
}
get(key) {
// implement this
}
hashStr(str) {
let finalHash = 0;
for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i);
finalHash += charCode;
}
return finalHash;
}
}
try {
var dict = new Hashmap();
OUTPUT
Results will appear here.