Background
I follow a course on Udemy that covers all the features of ES6. In one of the lessons, the teacher talks about using the auxiliary assistant method to solve the survey related to the mass survey.
I can solve this without a reduction method. Although using the reduction method, it does work in less code. I was asked to find the depth of parentheses in an interview before and wondered if all this can be done using the same method using the abbreviation.
I do not know why this addition to the question bothers me, but I would like to know.
Problem
I’ve been trying to figure this out for a while, and maybe I don’t understand how the reduction works.
Example
This uses a shorthand to return true for false if the bracket is either open and closed evenly.
function balanceParens(string) {
return !string.split("").reduce((counter, char) => {
// Handle if parens open and close out of order
if (counter < 0) { return counter; }
// Add 1 for each open in order
if (char === "(") { return ++counter; }
// subtract 1 for each close in order
if (char === ")") { return --counter; }
// handle use case if char is not a paren
return counter;
}, 0);
}
console.log(balanceParens("((()))"));
Question
How can I return the maximum bracket depth using the auxiliary reduction method.
source
share