Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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
    Last edited by papas; 12-09-2009 at 10:16 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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)
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts
    you are a life savior sir!!!!!!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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