Fix method Trie::doesWordExist() (#175)

Method Trie::doesWordExist() return `true` when word is complete otherwise `false`
This commit is contained in:
Oleg 2018-08-21 23:00:11 +03:00 committed by Oleksii Trekhleb
parent 5eb1195c61
commit 392cd9806d
2 changed files with 5 additions and 2 deletions

View File

@ -43,7 +43,9 @@ export default class Trie {
* @return {boolean} * @return {boolean}
*/ */
doesWordExist(word) { doesWordExist(word) {
return !!this.getLastCharacterNode(word); const lastCharacter = this.getLastCharacterNode(word);
return !!lastCharacter && lastCharacter.isCompleteWord;
} }
/** /**

View File

@ -45,7 +45,8 @@ describe('Trie', () => {
trie.addWord('caption'); trie.addWord('caption');
expect(trie.doesWordExist('cat')).toBe(true); expect(trie.doesWordExist('cat')).toBe(true);
expect(trie.doesWordExist('cap')).toBe(true); expect(trie.doesWordExist('cats')).toBe(true);
expect(trie.doesWordExist('cap')).toBe(false);
expect(trie.doesWordExist('call')).toBe(false); expect(trie.doesWordExist('call')).toBe(false);
}); });
}); });