Created
December 6, 2019 19:56
-
-
Save alonat/2f9ca48f01d50efa2a7e50c61c3ea1b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dictionary { | |
constructor() { | |
this.obj = {}; | |
this.dictionary = { | |
A: 2, | |
B: 2, | |
C: 2, | |
D: 3, | |
E: 3, | |
F: 3, | |
G: 4, | |
H: 4, | |
I: 4, | |
J: 5, | |
K: 5, | |
L: 5, | |
M: 6, | |
N: 6, | |
O: 6, | |
P: 7, | |
Q: 7, | |
R: 7, | |
S: 7, | |
T: 8, | |
U: 8, | |
V: 8, | |
W: 9, | |
X: 9, | |
Y: 9, | |
Z: 9 | |
}; | |
} | |
getHashByName(key) { | |
let hash = ""; | |
for (let i of key) { | |
hash += this.dictionary[i.toUpperCase()]; | |
} | |
return hash; | |
} | |
add({ name, phone }) { | |
const hash = this.getHashByName(name); | |
this.obj[hash] = this.obj[hash] || [] | |
this.obj[hash].push({ name, phone }); | |
} | |
findAll(keyStart) { | |
const resultArr = []; | |
Object.keys(this.obj).forEach(key => { | |
if (key.startsWith(keyStart)) { | |
resultArr.push(...this.obj[key]); | |
} | |
}); | |
return resultArr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment