Skip to content

Instantly share code, notes, and snippets.

@kutyel
Created August 11, 2023 09:42
Show Gist options
  • Save kutyel/5e02a9fecfb4c6556073a86eef2bd193 to your computer and use it in GitHub Desktop.
Save kutyel/5e02a9fecfb4c6556073a86eef2bd193 to your computer and use it in GitHub Desktop.
Haskell style valid braces kata but in JavaScript! 🥷🏻
const validBraces = (str) => ![...str].reduceRight(collapse, []).join('')
const collapse = ([head, ...tail], val) => {
switch (val) {
case '(':
return head === ')' ? tail : [val, head, ...tail]
case '{':
return head === '}' ? tail : [val, head, ...tail]
case '[':
return head === ']' ? tail : [val, head, ...tail]
default:
return [val, head, ...tail]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment