newbie here… I’m trying to grasp the concept of functional programming in Javascript, but I got stuck. I’m trying to apply a function to another function with recursion (higher-order function). Let’s say I have an input that can be a variable or an array, for example: My basic function should convert Fahrenheit to Celsius (but it could really be any
Tag: recursion
Get parent, grandparent and key in the deep nested object structure
I have a deeply nested structure in the javascript object without any arrays in it. When I call the function with a target, I want to get the taget key itself, parent and grandparent. For example, if the target is ‘c’, arr should become if target is ‘greatgrand’, it should become Thanks Answer I did it using your recursive pattern,
Recursion with map function in javascript
I have a map like this, which represents a graph: And I have this class, which creates a rooted tree: My goal is: given a root, I want to convert that graph to a rooted tree. For the example above, using 1 as root, I want the following return: Here’s my code: I’m receiving an empty array from the toRT
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
How can I print variables inside function every 3 seconds using recursion?
I wanna print variable inside function every 3 seconds using recursion (or loop) so I’ve tried to How can I fix that as I expect? Answer You need to make a couple of changes to your code: Your i is getting defined in every function execution, that is not what you want. It will never move to more than 1.
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 to keep the parent id of the children when creating an object recursively?
I have an object similar to this one: And I want to construct a tree from it: I write this code to create the tree: However, I’m stucked how to keep the track of the parent ids of a children and save them in a path attribute. How can I fix that? Answer You have a recursive function, so just
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