Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I am learning Javascript and TypeScript and got to the Recursion section of my course.

Now, I have this exercise, where I need to write a recursive function for changing specific characters into a different character.

Here is my code so far:

JavaScript
'use strict'

function strings1(str:string):any{
    try {
        let newStr:string = "";
        let changed = str.split('');
        console.log(str);        
        if (newStr == "") {
            changed.splice((changed.indexOf('x')), 1, "y");
            newStr += changed.join("");
        }
        return newStr;
    }
    catch (error)
    {
        console.log(error);
    }
}


What I have tried:

I tried to call the function inside a while loop, but that's not the solution.
I looked at the length of the input string then tried with charAt() and replace().
I have no idea how to write this recursively.
Posted
Updated 11-Jul-22 5:48am

1 solution

Recursion is when a function repeatedly calls itself with a modified form of its input parameter. See javascript recursion - Google Search[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900