Skip to content

Tag: recursion

Print a string from n through 1 using recursion

I’m trying to print a string from number n through 1 using this recursive function: I know changing the lines 7 and 8 to return outputStr + n + numStr(n-1) does the job. But I can’t figure out why the above code doesn’t work as expected. It prints ‘1’ in all cases. Answer When n!…

How to find path from tree of nodes

Consider a data structure similar to the one below: The function I have tried is as follows using recursion. I am getting an output: [{name: ‘Link10’, url: ‘link10’}] I would like to get an output as follows: And if I call the function as follows: Should return results as [{name: &#821…

How can I recurse in JS to repeat a string n times?

I am trying to make the code below repeat string s n number of times, however, it will always repeat n – 1 times, because I need to pass a decrement to exit the recursion or I will exceed the max call stack. What can I change to make it recurse correctly? Answer Recursion involves making a function call…