Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Balanced parentheses problem added #167

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

Closed
adarshaacharya wants to merge 1 commit into knaxus:master from adarshaacharya:master
Closed

Balanced parentheses problem added #167

adarshaacharya wants to merge 1 commit into knaxus:master from adarshaacharya:master

Conversation

Copy link

@adarshaacharya adarshaacharya commented Jul 6, 2020
edited
Loading

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

@adarshaacharya adarshaacharya Jul 26, 2020

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

@adarshaacharya adarshaacharya Jul 26, 2020

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

ashokdey reacted with eyes emoji
Copy link
Member

@ashokdey ashokdey Jul 26, 2020
edited
Loading

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

@adarshaacharya adarshaacharya Jul 26, 2020

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
Reviewers

@ashokdey ashokdey ashokdey requested changes

Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /