ребята! Я хочу спросить вас, как сделать функцию, которая проверяет, правильно ли расставлены скобки в строке. Например, "(a + b).4,2 - )c + 5)", и я должен проверить скобки. Я что-то пробовал, но это не работает (извините, я новичок в javascript):
function checkBrackets(str){
var newOrder = [];
var bracket1 = "(";
var bracket2 = ")";
for(var bracket1 in str){
newOrder.push("1");
}
for(bracket2 in str){
newOrder.pop();
}
if(newOrder.length == 0){
console.log("Right!" + newOrder);
} else{
console.log("Wrong!" + newOrder);
}
}
checkBrackets('( ( a + b ) / 5 – d )');
Я попытался перебрать строку с помощью цикла for-in и всякий раз, когда он попадает в «(», чтобы добавить «1» в массив. И когда он попадает в «)», чтобы удалить один «1» из массива. В конце, если массив пуст, я могу сделать вывод, что скобки расставлены правильно, а если нет, то нет.
.pop()
пустой массив. - person JJJ   schedule 20.12.2014(a + b).4,2 - )
, массив будет пустым, когда вы нажмете второй)
. - person JJJ   schedule 20.12.2014