Created
August 11, 2023 09:42
-
-
Save kutyel/5e02a9fecfb4c6556073a86eef2bd193 to your computer and use it in GitHub Desktop.
Haskell style valid braces kata but in JavaScript! 🥷🏻
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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