Create a Trie Search Tree fails print() & isWord() methods

Create a Trie Search Tree

Hi,
My second solution to try pass the test… still fails the last two… :frowning:
What should are wrong?

my code
class Node {
    constructor() {       
        this.keys = new Map();
        this.end = false;
        this.setEnd = function() {
            this.end = true;
        }
        this.isEnd = function() {
            return this.end;
        }
    }
}

class Trie {
    constructor() {
        this.root = new Node;
    }                                 
    
    add(word) {
        let loop = this.loop(word);
        let result = loop.next();
        let node = this.root;
        while (!(result.done)) {
            const {letter, index} = result.value;
            if (!(node.keys.has(letter))) node.keys.set(letter, new Node()); // set this new letter
            if (index === word.length - 1) node.setEnd(); // word end => set end to true
            node = node.keys.get(letter);
            result = loop.next();
        }
    }
    
    isWord(word) {
        let loop = this.loop(word);
        let isWord = false;
        let result = loop.next();
        let node = this.root;
        while (!(result.done)) {
            const {letter, index} = result.value;
            if (node && !(node.keys.has(letter))) {
                isWord = false;
                return isWord;
            }
            if (index === word.length - 1 && node.isEnd()) {
                isWord = true;
                return isWord;
            }
            node = node.keys.get(letter);
            result = loop.next();
        }
        return isWord;
    }
    
    print() {
        let words = [], subWord = "";
        const walkInTrie = (node = this.root, word = "") => {
            for (let letter of node.keys.keys()) {
                if (node.keys.size > 1) {
                    subWord ? subWord.slice(0, subWord.length -1) : subWord = word;
                    walkInTrie(node.keys.get(letter), subWord += letter);
                } else {
                    subWord = "";
                    walkInTrie(node.keys.get(letter), word += letter);
                }
                if (node.isEnd()) words.push(word);
            }
        };
        walkInTrie();
        return words.length > 0 ? words : null;
    }
    
    // callback: node, index, key,  stop(break the loop)
    * loop(word) {
        const letters = [...word];
        for (let index = 0; index < letters.length; index ++) {
            const letter = letters[index];
            const result = {index: index, 
                            letter: letter}
            ;
            yield result;
        }
    }
}

const myTrie = new Trie();
myTrie.add("code");
myTrie.add("coder");
myTrie.add("coala");
myTrie.add("csaba");
myTrie.add("csincsilla");
console.log(myTrie.isWord("code"));
console.log(myTrie.isWord("copied"));
console.log(myTrie.print());
console.log(myTrie);


I found the mistake. Resolved myself…

what is the correct?

Hi,
i needed to modify a bit the print method. I was near the final solution, my logic was wrong…
Needed to write one more if conditional…
Try to resolve yourself. If you really stack up then I will help you.
By the way, I highly recommend you to take some extra time and learn deeply these theme. The FCC explanation is a bit confusing. I found this blog. About 3-4 days needed to read and learn(?) everything from Adrian’s blog. If you read also everything from the above blog, you will have enough knowledge to resolve all of the FCC data structure challenge and in addition you will learn some new coding technics too.