As you can see, all hashStr
does is take the key
provided in set()
, and computes a location for us. We'll thus need another data structure
for the actual storage and buckets that the values are placed. Of course, you already know it's an array!
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.