1

So I am doing algorithm problems on AlgoExpert, and I came across a recursive DFS algorithm. Originally I tried solving it in JavaScript but was having issues. When I watched the code walk-through I saw that the way it was solved is super easy.

Here is the Python solution:

def depthFirstSearch(self, array):
    array.append(self.name);
    for child in self.children:
        child.depthFirstSearch(array);
    return array

My question is, how would you achieve the same thing in JavaScript? I tried doing:

function depthFirstSearch(array) {
    array.push(this.name);
    for(let i=0; i < array.length; i++) {
        depthFirstSearch(array[i]);
    };
    return array;
}

I keep getting an error:

depthFirstSearch is not defined

I'm confused about why I'm getting the error in JavaScript, when I'm trying to recursively call the function within itself?

Here is the whole code snippet:

class Node {
    constructor(name) {
        this.name = name;
        this.children = [];
    }

    addChild(name) {
        this.children.push(new Node(name));
        return this;
    }

    depthFirstSearch(array) {
       /*
        * Step 1: Add the root node to the array
        *                   this.name is the root at the start
        * Step 2: Check that the left child node isn't null
        * Step 3: If the left child node isn't null, Add the left child node to the array
        * Step 4: If the left child node is null check the right child node
        * Step 5: If the right child node isn't null, add the right child node to the array
        * Step 6: Repeat steps 1-5 until there are no nodes left to check
        */
        array.push(this.name);
        const children = this.children;
        for(let i=0; i < array.length; i++) {
            depthFirstSearch(array[i]);
        };
        return array;
    }
}
3
  • 3
    Random thought, but have you tried calling this.depthFirstSearch(array[i])? Because your function is defined in a class, it isn't accessible by just calling depthFirstSearch. Commented Apr 24, 2021 at 13:54
  • Look at the scope of your function, and then see how to refer methods of the same class Commented Apr 24, 2021 at 14:49
  • @ImranAhmed I did try using this.depthFirstSearch(array[i]) and it said that array was undefined. Commented Apr 24, 2021 at 20:23

1 Answer 1

1

Several issues:

  • depthFirstSearch is a method of the Node class, not a global function, so it should be called on a node instance. So for instance, if you have a node named child it should be called like child.depthFirstSearch

  • As it is a method, you should not use the function keyword (in a class construct). This was correctly removed in the last code block you have in your question.

  • The argument passed to depthFirstSearch should be an array, but you pass it array[i] which is a node. It should be just array, as in the Python code.

  • The loop should not be over array, but over the children of the current node, i.e. over this.children. So you would have i < this.children.length in your for loop.

Some other remarks:

  • No semi-colon is needed after a for loop. Adding it actually introduces a (harmless) empty statement.
  • Using a for..of instead of a plain old for loop makes the code a bit shorter and also better mimics what the Python code does.

Corrected version:

class Node {

    /* ... the other code ... */

    depthFirstSearch(array) {
        array.push(this.name);
        for (let child of this.children) {
            child.depthFirstSearch(array);
        }
        return array;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This worked. I forgot about for...of loops in JS, so thanks for the refresher. Also, thanks for expanding my knowledge of JS!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.