Skip to content Skip to sidebar Skip to footer

Is This An Issue In My Recursive Function?

I need to run through a stack of HTML Elements. But all my attempts to write a recursive function didn't work. In the snippet below I cannot return value from if statement and aft

Solution 1:

Using the return keyword like you are doing.

Your second function calls the first function without returning the value returned by the first function:

Replace

    if (element.children && typeof element === 'object') {
      findElementByDataValue(element, data);
    }

with:

    if (element.children && typeof element === 'object') {
      return findElementByDataValue(element, data);
    }

In general, run your code in a debugger (popular web browsers provide debuggers) to see what is going on.

See some debuggers documentation:

If you are new to JavaScript, I suggest looking into unit testing and test-driven development.

Writing tests (early) will help you think of what can go wrong with your code and write more robust functions. Jasmine is nice, this article suggests many other JavaScript unit testing options


Post a Comment for "Is This An Issue In My Recursive Function?"