Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: recursion problem

1. ## recursion problem

Code:
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

• 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:
Code:
}
return checkifischild(draggedElement,parent)
}

• you are a life savior sir!!!!!!

• 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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•