This code is very confused. You define a Node class and only use it once. Further code reverts to using plain objects. You might want to revise this example to either use the Node class throughout (and its data, children properties etc.), or just use plain objects throughout (remove the Node class, and make root {}).
You've also stated that:
A Trie is a data structure thats main purpose is retrieval
But you haven't shown an example of how that can be done! (Hint: the original Node class might be useful for this, if fully integrated)
Other issues:
The search function returns null or true instead of true/false as stated
The node !== null part of the return expression is entirely redundant, since it is always true
Even if you remove the redundant first term of the expression, the remainder still has redundancy. You could simply just return node.isEndOfWord (although this would still be returning null/true - but this isn't a big issue as JS has the concept of truthiness and falsiness)
UPDATE: On further inspection, it's more luck than judgement that the search method even works - especially where you are checking for === null - you won't find a null anywhere. Overall, your code will be finding a lot of undefineds everywhere
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This code is very confused. You define a
Nodeclass and only use it once. Further code reverts to using plain objects. You might want to revise this example to either use theNodeclass throughout (and itsdata,childrenproperties etc.), or just use plain objects throughout (remove theNodeclass, and make root{}).You've also stated that:
But you haven't shown an example of how that can be done! (Hint: the original
Nodeclass might be useful for this, if fully integrated)Other issues:
searchfunction returnsnullortrueinstead oftrue/falseas statednode !== nullpart of the return expression is entirely redundant, since it is alwaystruenode.isEndOfWord(although this would still be returningnull/true- but this isn't a big issue as JS has the concept of truthiness and falsiness)UPDATE: On further inspection, it's more luck than judgement that the
searchmethod even works - especially where you are checking for=== null- you won't find anullanywhere. Overall, your code will be finding a lot ofundefineds everywhere