...

View Full Version : Resolved recursion problem



papas
12-09-2009, 09:12 PM
function checkifischild(draggedElement,destination)
{
$getid = $("#" + destination).attr("class");
var __temp = $getid.split(' ');
console.log(__temp)
for(i = 0;i < __temp.length; i++){//for the length of the classes
if(__temp[i].match("child-of-"))
{//check if its a child
parent = __temp[i].substring(9);//get the parents node
if(draggedElement == parent)
{
console.log('yes');
return false;
}
}

}
checkifischild(draggedElement,parent)
}


i have the following function

basically what it does it gets 2 parameters
node-23,node-17 for example

it finds the classes of node-17 and splits them up
["child-of-node-16", "initialized", "ui-droppable", "expanded"]
["child-of-node-19", "initialized", "parent", "expanded", "ui-droppable"]
["child-of-node-23", "initialized", "parent", "expanded", "ui-droppable"]

so i compare each time the first parameter along with the substring of the child-of and if they are the same its returns false

now my problem is if i call the function just like this

checkifischild(node-23,node-17) it works i get in the console yes for example

if i do this tho if(checkifischild(node-23,node-17) == false) it fails :?
any ideas will be appreciated thxxx

Old Pedant
12-09-2009, 10:13 PM
You never return any value from any "level" of the recursion except the last.

So if you are 3 levels deep, say, you return false to the 2nd level, but then it returns nothing to the 1st level which returns nothing to the caller.

Just change the last line of the function:

}
return checkifischild(draggedElement,parent)
}

papas
12-09-2009, 10:16 PM
you are a life savior sir!!!!!!

Old Pedant
12-09-2009, 10:19 PM
Seems kind of pointless, though. You never return, at all, unless you return false. So the return value will always be false.

Seems to me like the only other way out of the function is an error when you run out of parents???

And the weird part is that you recurse only for the *last* parent value found in that "for ( i...)" loop.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum