Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Balanced parentheses problem added #167

wants to merge 1 commit into from

Conversation

adarshaacharya
Copy link

@adarshaacharya adarshaacharya commented Jul 6, 2020

Issue no : #166

Solving balanced paratheses problem using reduce() method in ES6.

@adarshaacharya adarshaacharya changed the title Balanced parentheses problem added #166 Balanced parentheses problem added Jul 6, 2020
Copy link
Member

@ashokdey ashokdey left a 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 💯

Comment on lines +15 to +21
if (prev < 0) return prev;

if (char === '(') return ++prev;

if (char === ')') return --prev;

return prev;
Copy link
Member

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?

Copy link
Author

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 ?

Copy link
Author

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);

Copy link
Member

@ashokdey ashokdey Jul 26, 2020

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

Copy link
Author

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) {
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants