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

3
  • 3
    You don't return anything in the true-case. Commented Sep 27, 2019 at 20:16
  • return testTokenRemoved(....) Commented Sep 27, 2019 at 20:18
  • That is by design. It only gets returned if it fails 3 times or passes. Commented Sep 27, 2019 at 22:08

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

5 Comments

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?
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 ==> imgur.com/MVQy4CW
@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.
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?
@MichaelJohns Exactly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.