JavaScript JS function for validation of the brackets in a string

guys! I want to ask you how can a make a function, that checks if the brackets in a string are put correctly. For example "(a + b).4,2 - )c + 5)" and I have to check the brackets. I tried something, but it doesn't seem to work(sorry, I'm a newbie in 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 )');
Answer:1

You can do it this way :

// str is the string to parse
function checkBrackets(str){
    // depth of the parenthesis
    // ex : ( 1 ( 2 ) ( 2 ( 3 ) ) )
    var depth = 0;
    // for each char in the string : 2 cases
    for(var i in str){   
        if(str[i] == '('){
            // if the char is an opening parenthesis then we increase the depth
            depth ++;
        } else if(str[i] == ')') {
            // if the char is an closing parenthesis then we decrease the depth
            depth --;
        }  
        //  if the depth is negative we have a closing parenthesis 
        //  before any matching opening parenthesis
        if (depth < 0) return false;
    }
    // If the depth is not null then a closing parenthesis is missing
    if(depth > 0) return false;
    // OK !
    return true;
}
console.log(checkBrackets('( ( a + b ) / 5 – d )')); // true
console.log(checkBrackets('( ( ) a + b ) / 5 – d )')); // false
console.log(checkBrackets('( ) ) ( ( a + b ) / 5 – d )')); // false
Answer:2

While you've accepted an answer already, I felt it was a little complex, so I thought I'd expand on the naive solution I presented in the comments to the question:

function checkBrackets(str) {
  // using a regular expression to find the number '(' characters in the string,
  // escaping with a '\' because '(' is a special character within a regular
  // expression; if no matches are found, and the result of 'match()' is falsey,
  // we instead assign an empty array (in order that calling 'length', later, on
  // a potentially-null object, won't create an error):
  var opens = str.match(/\(/g) || [],
    closes = str.match(/\)/g) || [];

  // if there are equal numbers of '(' and ')' characters, we return true,
  // otherwise false:
  return opens.length === closes.length;
}

// unnecessary, this is just a means of iterating over the <li> elements, to work on
// a range of inputs for the function:
Array.prototype.forEach.call(document.getElementsByTagName('li'), function(li) {
  // li is the <li> element/node itself, li.textContent is the text contained within
  // that <li>, using the classList API to add a 'valid' class (if brackets are balanced)
  // or an 'invalid' class (if the brackets are not balanced):
  li.classList.add(checkBrackets(li.textContent) ? 'valid' : 'invalid');
});
ul {
  margin: 0;
  padding: 0;
}
li {
  list-style-type: none;
  margin: 0 0 0.5em 0;
  padding: 0.5em;
  width: 100%;
  box-sizing: border-box;
}
.valid {
  border: 1px solid #0f0;
}
.invalid {
  border: 1px solid #f00;
}
<ul>
  <li>( ( a + b ) / 5 - d )</li>
  <li>( a + b ) / 5 - d</li>
  <li>( a + b ) / ( 5 - d )</li>
  <li>( a + b ) / 5 - d )</li>
  <li>a + b ) / 5 - d</li>
  <li>( a + b / 5 - d</li>
</ul>
Answer:3

I am using React's PropTypes and Flow type checker but I am having trouble getting an optional function prop to pass the type check. Here is an example: var Example = React.createClass({ propTypes: ...

I am using React's PropTypes and Flow type checker but I am having trouble getting an optional function prop to pass the type check. Here is an example: var Example = React.createClass({ propTypes: ...

  1. react optional function prop
  2. typescript optional function prop
  3. javascript function optional prop

I have a select drop down filled with options and then I have two radios like so... <form> <select id="drop_down"> <option value="1">One</option> <option value="2"...

I have a select drop down filled with options and then I have two radios like so... <form> <select id="drop_down"> <option value="1">One</option> <option value="2"...

I have an object similar to this: $scope.division = [ { name: "Tom", number: 1 }, { name: "Greg", number: 2 } ]; Now in my HTML I'm using ngOption to display a drop down ...

I have an object similar to this: $scope.division = [ { name: "Tom", number: 1 }, { name: "Greg", number: 2 } ]; Now in my HTML I'm using ngOption to display a drop down ...

I wish to have a row of 3 images which will contain around 10 images per row. The image dimensions are 270x270px and should animate like you'd imagine clouds floating from right to left on the screen....

I wish to have a row of 3 images which will contain around 10 images per row. The image dimensions are 270x270px and should animate like you'd imagine clouds floating from right to left on the screen....