Skip to content
Advertisement

Tag: recursion

why isn’t recursion working? i am trying to display n to 1 series

Javascript Recursion why isn’t recursion working here? i see coundown(n-1); isn’t working for some reason. I want to display [5,4,3,2,1] Answer Your code creates a new array at every recursive call, puts one value in it and returns it. Nothing is done with the array that is returned, as each execution instance of your function seems only interested in its

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!=1 then you are just appending

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: ‘Link11’, url: ‘link11’,}] I tried the following as well and

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 itself

CodeSignal reverseParentheses Failing one case

Write a function that reverses characters in (possibly nested) parentheses in the input string. Input strings will always be well-formed with matching ()s. For inputString = “(bar)”, the output should be reverseInParentheses(inputString) = “rab”; For inputString = “foo(bar)baz”, the output should be reverseInParentheses(inputString) = “foorabbaz”; For inputString = “foo(bar(baz))blim”, the output should be reverseInParentheses(inputString) = “foobazrabblim”. [input] string inputString A

Advertisement