1

This is test code. I'm not sure why I'm not getting the string returned on line console.log("hasToken : " + hasToken);? I should get it returned after 3 failed attempts. The string without the tokens (var XX) works fine but the string with the token (var X) never returns the string properly. If I step through the code I see the string being passed back but something else happens and I don't know why? The console.log shows undefined for the hasToken value? I see where the string is suppossed to be returned.Test Code Here

var x = "//tokene.secure.adnxs.com/px?randomnumber=[timeSTAMP]&url=[URl]&newvsreturning=[NEWvsRETURNING]";
var xx = '//xwww.googleadservices.com/pagead/conversion/822232847/?label=_7yrCLe91H4Qj46JiAM&guid=ON&script=0';

var regex = new RegExp("/\\[URL\\]|\\[NEWVSRETURNING\\]|\\[RANDOMNUMBER\\]|\\[TIMESTAMP\\]|\\[CACHEBUSTER\\]/", "i");


function validateTokenRemoval(str) {
cnt = 0;
    function testTokenRemoved(str) {
        if ((regex.test(str))&&(cnt < 2)) {
            //if in here it still has tokens
   cnt++;
            //str = detokenizeTags(imgSrc);
   console.log(cnt + " failed : " + str);
            testTokenRemoved(str);
            
        } else {
            console.log(cnt + " passed : " + str);
            return str;
        }
    };
 
 return testTokenRemoved(str);
};

var hasToken = validateTokenRemoval(x);
var noToken = validateTokenRemoval(xx);
console.log("hasToken : " + hasToken);
console.log("noToken : " + noToken);
You can see my last step in the debug process Return Debug image
Michael Johns
  • 419
  • 3
  • 21

1 Answers1

1

You have to put a return statement before the testTokenRemoved(str) in your if block, to return the return value of the recursive call:

var x = "//tokene.secure.adnxs.com/px?randomnumber=[timeSTAMP]&url=[URl]&newvsreturning=[NEWvsRETURNING]";
var xx = '//xwww.googleadservices.com/pagead/conversion/822232847/?label=_7yrCLe91H4Qj46JiAM&amp;guid=ON&amp;script=0';

var regex = new RegExp("/\\[URL\\]|\\[NEWVSRETURNING\\]|\\[RANDOMNUMBER\\]|\\[TIMESTAMP\\]|\\[CACHEBUSTER\\]/", "i");


function validateTokenRemoval(str) {
cnt = 0;
    function testTokenRemoved(str) {
        if ((regex.test(str))&&(cnt < 2)) {
            //if in here it still has tokens
   cnt++;
            //str = detokenizeTags(imgSrc);
   console.log(cnt + " failed : " + str);
            return testTokenRemoved(str);
            
        } else {
            console.log(cnt + " passed : " + str);
            return str;
        }
    };
 
 return testTokenRemoved(str);
};

var hasToken = validateTokenRemoval(x);
var noToken = validateTokenRemoval(xx);
console.log("hasToken : " + hasToken);
console.log("noToken : " + noToken);

Ok, but why did the inspector misinformed you? Let's look inside:

  • On the screenshot, the inspector isn't paused at the statement, what you try to get the value of. You may think, that isn't an issue, and it isn't, until they are in the same scope.
  • Because the inspector paused outside the function you want to inspect, the str value of that isn't in-scope anymore.
  • Finally, because you have a different variable named str in the outer scope, the inspector will show its value to you, while the value you want to inspect is already undefined.
FZs
  • 16,581
  • 13
  • 41
  • 50
  • Thanks for the response. I'm still confused. The string with no token (var xx) that passes the first time is returned as expected. How come the string with the tokens which runs through the check in the testTokenRemoved function doesn't work once it reaches the final attempt and is returned? – Michael Johns Sep 27 '19 at 22:07
  • I change the name of the sub-routine argument for clarity. I retested and still see the string being passed back but for some reason, the function is not returning the value to the variable image is here ==> https://imgur.com/MVQy4CW – Michael Johns Sep 27 '19 at 23:40
  • @MichaelJohns Sure, because you inspect the `str` variable, not `testTokenRemoved`'s return value. Select the full `testTokenRemoved(str)` expression with mouse and hover over it. You'll see that the value is returned exactly as it is logged later. – FZs Sep 28 '19 at 05:01
  • So the issue was the way I was doing my recursion but I don't understand why I saw the value being returned from the function? – Michael Johns Sep 28 '19 at 08:52
  • @MichaelJohns Exactly. – FZs Sep 28 '19 at 08:53