We can move onto the search
method!

This is similarly straightforward. The key part is again testing our currentChar
/current character against the children
hash's keys. If our current character is a child of the current node, then we can traverse to that child and see if the next letter matches as well.
Note that in our example, we're keeping track of depth and returning it. You can do this, or return the actual word itself once you hit the end of a word.
xxxxxxxxxx
67
}
class Trie {
constructor() {
this.head = {
val: '',
children: {},
};
}
add(val) {
let currentNode = this.head,
newNode = null,
currentChar = val.slice(0, 1);
val = val.slice(1);
// See how deep we can get in any existing branches
while (
typeof currentNode.children[currentChar] !== 'undefined' &&
currentChar.length > 0
) {
currentNode = currentNode.children[currentChar];
currentChar = val.slice(0, 1);
val = val.slice(1);
}
// Break each character into levels respectively
// 'val' is the character, and 'value' lets us know if it terminates
OUTPUT
Results will appear here.