How to Preserve Initial Parameters During Recursive Function Calls in JavaScript

I’m working on a string comparison function that needs to handle alphanumeric sorting with special rules for leading zeros. The tricky part is that I need to keep track of the original input values throughout multiple recursive calls.

Here’s my situation: I have a function that compares strings by ignoring leading zeros first. If all parts match, then I want to compare again but this time including the leading zeros to determine which string is actually smaller.

The problem happens when I try to pass the original arguments through recursion. Each time the function calls itself, the ‘original’ parameters get overwritten with values from the previous iteration instead of staying as the true original inputs.

function compareStrings(str1, str2, original1, original2) {
    console.log(str1, str2);
    if(str1.length < 1 && str2.length >= 1){
        return true;
    }
    var pattern = /(\D|[1-9])(.*)/;
    var match1 = pattern.exec(str1);
    var match2 = pattern.exec(str2);

    if(match1[1] == match2[1]){
        console.log('recursive call needed');
        return compareStrings(match1[2], match2[2], str1, str2);
    } else if(match1[1] < match2[1]){
        return true;
    } else {
        return false;
    }
}

How can I make sure the original arguments stay unchanged through all the recursive calls? I want to use them only at the very end when I need to do the final comparison with leading zeros included.

hey, you’re totally on the right track! just make sure you pass original1 and original2 instead of str1 and str2 in your recursive calls. like this: return compareStrings(match1[2], match2[2], original1, original2); it’ll keep the originals intact and fix your issue.

You’re hitting a classic recursion trap. When you pass str1 and str2 as the third and fourth parameters, you’re overwriting the original values with whatever’s left from each iteration. Here’s a cleaner fix - use default parameters: javascript function compareStrings(str1, str2, original1 = str1, original2 = str2) { // your existing logic here if(match1[1] == match2[1]){ return compareStrings(match1[2], match2[2], original1, original2); } // rest of your code } On the first call, original1 and original2 automatically grab the initial str1 and str2 values. Then in your recursive calls, you just keep passing those originals forward unchanged. Now you’ll always have access to the starting strings for your final comparison.

Your problem is with how you’re passing parameters in the recursion. Don’t change your function signature - just store the original values outside the recursive chain. Use a closure or helper function.

function compareStrings(str1, str2) {
    function recursiveCompare(current1, current2, orig1, orig2) {
        if(current1.length < 1 && current2.length >= 1){
            return true;
        }
        var pattern = /(\D|[1-9])(.*)/;
        var match1 = pattern.exec(current1);
        var match2 = pattern.exec(current2);

        if(match1[1] == match2[1]){
            return recursiveCompare(match1[2], match2[2], orig1, orig2);
        }
        // your comparison logic here
    }
    
    return recursiveCompare(str1, str2, str1, str2);
}

This captures the original strings at the first call and keeps them unchanged through the whole recursion. The inner function can access both the changing comparison state and the original inputs for your zero-aware comparison.

This topic was automatically closed 6 hours after the last reply. New replies are no longer allowed.