DEV Community

Discussion on: Daily Challenge #51 - Valid Curly Braces

Collapse
 
alfredosalzillo profile image
Alfredo Salzillo

Another one line JS solution

const areCurlyBracesMatched = string => !Array(Math.flor(string.length / 2))
  .fill(0)
  .reduce(s => s.replace(/{}/, ''), string);

Collapse
 
aminnairi profile image
Amin

You made a typo: Math.flor should be Math.floor. Otherwise good take at using a regular expression. Unfortunately, it does not work for cases like

areCurlyBracesMatched("const f = a => { console.log(a)}")

As the curly braces are separated by other characters.

Collapse
 
alfredosalzillo profile image
Alfredo Salzillo

Write a function areCurlyBracesMatched that takes in a string s containing only { and/or } and returns true if s is properly matched, and false otherwise.

For this challenge the string can only contain '{' or '}', so your example is not a valid input.

It's obvious that it can't be resolved with a regexp for the general case.

Collapse
 
avalander profile image
Avalander

Smart! I like it!