# Is recursion the right method here and if so what am I missing?

The function I’m writing has to accomplish the following.

It needs to find all parenthesis in an array and create the correct groupings.

Array examples include

``````["(", "(", ")", ")"]
["(", "(", ")", "(", ")", ")"]
``````

Eventually there will be numbers nestled in them as well.

``````["2", "+", "5", "**", "3", "(", "57", "/", "3", ")"]
``````

I’m going through my current code and it’s recursive, but it’s not really doing much except updating the global parenthesisIndexArray. At the moment, I’m just looking for advice on improving the code as well as how can I reliably and consistently say with complete confidence that an opening bracket belongs to a specific closing bracket? Debugging it has been quite fun and I’ve made good progress, but I’m still not able to put it all together just yet.

``````let parenthesisIndexArray = [];

function recursionBaby(arr, length, balance, openCount, closeCount) {
// Base case

if(balance === 0 && length <= 0) {
if(arr[length] === "(") {
balance--;
openCount++;
parenthesisIndexArray.push({
value: arr[length],
index: length
})
}
else if(arr[length] === ")") {
balance++
closeCount++
parenthesisIndexArray.push({
value: arr[length],
index: length
})
}
else {
console.log("balance didn't change");
}
return parenthesisIndexArray;
}
else {
if(arr[length] === "(") {
balance --;
openCount++;
parenthesisIndexArray.push({
value: arr[length],
index: length
})
}
else if(arr[length] === ")") {
balance++
closeCount++
parenthesisIndexArray.push({
value: arr[length],
index: length
})
}
return recursionBaby(arr, length-1, balance, openCount, closeCount)
}
}
``````

My first recommendation would be not to use a global array.

1 Like

Everytime I tried to hold the array in the function it wasn’t recognised by the if statement though

I tried Bruce…

Hello @Codemiester, you need to visit each element of the array. There are differents ways to do this:

Using the built-in methods, an example:

``````const arr = [1,1,1]
const sum = (a) =>  a.reduce((a,b)=> a+b,0)
console.log(sum(arr)) // 3
``````

The same using recursion:

``````const arr = [1,1,1]

const sum = a => {
if(a.length === 0) return 0
const [first, ...rest] = a
return first + sum(rest)
}

console.log(sum(arr)) // 3
``````

The first and rest approach has some variations, but that is the idea.

You also can use an index for the the recusive function, but I think that is confusing.

The problem description is insufficient, (the input is an array ? the output is also an array?, what is the meaning of “grouping”? the output is a nested array?). Also, there are not enough examples (the examples should show the expected input and the expected output, of different cases [what is the expected output of an empty array? or the expected output of this [“(”,“)”,“)”,“)”])

Cheers and happy coding

Thanks for that Diego. I will continue getting better at providing more descriptive posts. I think they can be more concise and more informative. This is good feedback. I’ve fixed the issue now, so not a problem.

Genuinely appreciate you taking the time to help me there and I agree that I wasn’t as clear as needed for more detailed help.

Happy coding to you to

1 Like

As @Diego_Perez said, there are built in methods that are useful, but I’m going to assume that you don’t want to use those because of the type of learning exercise this is.

The decision on whether to solve a problem recursively or with a loop should come down to which results in simpler and more readable code. While this exercise wouldn’t be too bad to do recursively, it might be more intuitive for you to solve with a loop. The fact that you’re using a global array is a red flag that the way you’re thinking about the problem is non-recursive. While technically any function that calls itself is called “recursive”, when we talk about recursive solutions, we really mean purely functional ones. They should never depend on data in a higher scope.

3 Likes

That’s really interesting. Thanks for that input Ariel. You all remind me that I’ve still got so much to learn.

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