Jump to content

non AJAX function returns undefined


Muddy_Funster
Go to solution Solved by kicken,

Recommended Posts

ok guys, I'm a little confuddled about this one.  I'm not overly good at js, but have the need to do little bits and pieces here and there.  I have tried to create a few functions that should, when run together, recursively check through the given string and replace certain key character combinations and replace them for a specific value, which would be determined by runtime variables in the actual finished script.  My problem is that no matter what I try, the function is returning "undefined" rather than the data it has processed.  I have a fiddle of this when you can see it in action located here : http://jsfiddle.net/jJBH6/1/ - forgive the alerts, they are my preferred method of debugging.  for those link shy, I have posed the offending functions underneath.  I would really love if someone could explain why this is not returning the data as expected.

function checkHotVar(data) {
    var cursor = data.indexOf(':{');
    alert("special char search result = " + cursor);
    if (String(cursor) == '-1') {
        alert("returning ---" + data);
        return String("bugger me");
    } else {
        return ;
        validateHotVar(cursor, data);
    }
}

function validateHotVar(cuStart, data) {
    alert("moved into validate function with a starting point of " + cuStart);
    cuStart = cuStart;
    var cuEnd = data.indexOf('}', cuStart);
    if (String(cuEnd) == '-1') {
        alert("end Marker Not Found");
        return data;
    } else {
        cuEnd = cuEnd + 1;
        alert(" end marker found at " + cuEnd);
        datastring = data;
        var control = datastring.substring(cuStart, cuEnd);
        switch (control) {
            case ":{uid}":
                var repWith = 1;
                alert("match found swaping with " + repWith);
                break;
        }
        alert("starting swap function");
        doHotSwap(data, control, repWith);
       
    }
}

function doHotSwap(data, rep, wth) {
    alert("performing swapout process, changing " + rep + " with " + wth);
    var neData = data.replace(rep, wth);
    alert("swap performed, data now reads ---\n " + neData + "\n --- going back to check again");
    checkHotVar(neData);

}
Link to comment
Share on other sites

Thanks kick, that's done the trick.  My confusion came from the data being accurate going into each of the functions, because I was passing it in as parameters, but without the returns that you pointed out, it was never being released from the functions in a usable way.  My major mistake was thinking that the only point I needed to return the data was when I was breaking out of the functions altogether, rather than treating each function as needing to return the data independently.

 

Thanks for the insight kicken.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.