Jump to content


Photo

non AJAX function returns undefined


Best Answer kicken, 19 November 2013 - 03:07 PM

You're missing your return statements on lines 32 & 41. The return you have on line 8 should not have a ; after it, and should combine with line 9. Go to the full post


  • Please log in to reply
2 replies to this topic

#1 Muddy_Funster

Muddy_Funster

    Advanced Member

  • Members
  • PipPipPip
  • 3,383 posts

Posted 19 November 2013 - 10:51 AM

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);

}

Please: "This doesn't work..." is not a question.  We're not the government: we don't have anything to view what your doing on your computer.  Help us help you by asking a good question.

 

"Things needed to ask a "good" question:

  • A description of the context of your issue (Optional but can be a huge help in certain circumstances)
  • What is the actual problem (Mandatory)
  • What does the code actually do (Mandatory)
  • What you think the code should do / What you want the code to do (Mandatory)
  • What things have you tried so far (Optional, but missing it out just wastes your time and ours)
  • The actual code as you are running it - minus any personal information like Database Login Credentials (Mandatory - don't just post pseudo, the vast majority of issues are syntax and not logic)
  • As much info about your development environment as you can give - even if it's just letting us know you are using a hosting provider instead of a local install (Optional, but some questions can not be answered without it.)

#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,983 posts
  • LocationBonita, FL

Posted 19 November 2013 - 03:07 PM   Best Answer

You're missing your return statements on lines 32 & 41. The return you have on line 8 should not have a ; after it, and should combine with line 9.
Recycle your old CD's, don't trash them!
Did I help you out? Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 Muddy_Funster

Muddy_Funster

    Advanced Member

  • Members
  • PipPipPip
  • 3,383 posts

Posted 19 November 2013 - 03:37 PM

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.


Please: "This doesn't work..." is not a question.  We're not the government: we don't have anything to view what your doing on your computer.  Help us help you by asking a good question.

 

"Things needed to ask a "good" question:

  • A description of the context of your issue (Optional but can be a huge help in certain circumstances)
  • What is the actual problem (Mandatory)
  • What does the code actually do (Mandatory)
  • What you think the code should do / What you want the code to do (Mandatory)
  • What things have you tried so far (Optional, but missing it out just wastes your time and ours)
  • The actual code as you are running it - minus any personal information like Database Login Credentials (Mandatory - don't just post pseudo, the vast majority of issues are syntax and not logic)
  • As much info about your development environment as you can give - even if it's just letting us know you are using a hosting provider instead of a local install (Optional, but some questions can not be answered without it.)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users