-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Balanced parentheses problem added #167
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do look into the required changes. Thanks for the PR 💯
if (prev < 0) return prev; | ||
|
||
if (char === '(') return ++prev; | ||
|
||
if (char === ')') return --prev; | ||
|
||
return prev; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use a switch case here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think switch case isn't needed here since we have to check two variables char & prev here. can i use ternary operator instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this
return !string.split('').reduce((prev, char) => {
const result = prev < 0 ? prev : char === '(' ? ++prev : char === ')' ? --prev : prev;
return result;
}, 0);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above snippet is not readable enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, if else pattern will be fine right ? switch case will add more code since we have to check cases for prev & char.
*/ | ||
|
||
function balancedParantheses(string) { | ||
return !string.split('').reduce(function(prev, char) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about an arrow function here?
Issue no : #166
Solving balanced paratheses problem using reduce() method in ES6.