Created
February 15, 2016 10:03
-
-
Save Drugak/9b044450e3f70d54e986 to your computer and use it in GitHub Desktop.
Find correct close brackets
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
function findBrackets(str){ | |
var brackets = "{}[]()"; | |
var result = []; | |
var position; | |
for (var i = 0, max = str.split('').length; i < max; i++) { | |
position = brackets.indexOf(str.split('')[i]); | |
if (position > -1) { | |
if (position % 2 === 0) { | |
result.push(position + 1); | |
} else if (result.pop() !== position) { | |
return false; | |
} | |
} | |
} | |
return result.length === 0; | |
} | |
console.log(findBrackets("(2+2)*{fgdfg/dfgh//}{{{}aaa")); | |
console.log(findBrackets("(2+2)*{fgdfg/dfgh//}{}aaa")); | |
console.log(findBrackets("(2+2())*{fgdfg/dfgh//}{}aaa")); | |
console.log(findBrackets("(2+2{)}*{fgdfg/dfgh//}{}aaa")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment