Muddy_Funster Posted November 19, 2013 Share Posted November 19, 2013 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); } Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted November 19, 2013 Solution Share Posted November 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 19, 2013 Author Share Posted November 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.