123456789101112131415161718192021222324252627282930 |
- <header>
- <h1>For Brackets Hell Check</h1>
- </header>
- <body>
- <script>
- const line = prompt();
- const bracketsStack = [];
- const openBrackets = ["[", "(", "{"];
- const closeBrackets = ["]", ")", "}"];
- let i = 0;
- for (let character of line) {
- //не обращайте внимания на символы, кроме трех видов скобок
- if (openBrackets.includes(character))
- bracketsStack.push(character);
- else {
- idx = closeBrackets.indexOf(character);
- if (idx >= 0) {
- if (bracketsStack.length == 0 || bracketsStack.pop() != openBrackets[idx]) {
- alert("Wrong brackets consistency in position: " + i)
- break; //оператор break прерывает цикл преждевременно
- }
- }
- }
- i++;
- }
- </script>
- </body>
|