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:
- https://developer.mozilla.org/en-US/docs/Tools/Debugger
- https://developers.google.com/web/tools/chrome-devtools/javascript/
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
9
solved Is this an issue in my recursive function?