Javascript Recursion returning undefined -
i'm struggling in recursive javascript function find specific subdirectory. code:
function navigatetoparent() { var parentfullpath = parentdirectory(); // gets full path string if (parentfullpath != null) { var parent = getdirectorybyname(parentfullpath, rootdirectory); // set parent directory object current 1 currentdirectory(parent); } } function getdirectorybyname(fullname, mydirectory) { if (mydirectory.fullname == fullname) { return mydirectory; } else { var subs = mydirectory.subdirectories; (i = 0; < subs.length; i++) { return getdirectorybyname(fullname,subs[i]); } } }
every directory object has properties fullname
(string),subdirectories
(array of directories) , files
(array of files). aim correct directory object, it's fullname matching.
i know, have break loop in way, don't know how exactly.
after overthinking logic came solution (seems work):
function getdirectorybyname(fullname, mydirectory) { if (mydirectory.fullname == fullname) { return mydirectory; } else { var subs = mydirectory.subdirectories; (i = 0; < subs.length; i++) { var match = getdirectorybyname(fullname, subs[i]); if (typeof match !== "undefined"){ return match; } } } }
Comments
Post a Comment