[Recursion] Why returns an undefined value from recursion ?

Discussion in 'JavaScript' started by michaeldigital, Nov 21, 2007.

  1. #1
    hi ...
    here is a recursion , suppose it will return a value and
    but,,,when the function returned with a value ,and a var incept it,
    still it is null or undefined..
    the code here:

    function invokeRec () {

    var v = dataQuery(data,query) ; //assume that data and query are available , i.e. data is an array with some elements and query is a valid term
    alert(v); //
    }

    function dataQuery(data,query) {

    if(typeof(data[query]) != "undefined") {
    alert(data[query]) ; // here is valid and then return ...
    return (data[query]);
    }

    for(var something in data) {
    if ( typeof(data[something]) == "object"
    && typeof(data[something] != "undefined")
    && something != query) {
    dataQuery(data[something],query);
    }
    }
    }

    and here is the version2 :
    function dataQuery(data,query) {
    for(var something in data) {
    if ( typeof(data[something]) == "object"
    && typeof(data[something] != "undefined")
    && something != query) {
    dataQuery(data[something],query);
    } else if (something == query) {
    return (data[query]);
    }
    }
    }


    what's the problem with these two snippets of code?
    Thank you!
     
    michaeldigital, Nov 21, 2007 IP