Formulas are checked for correct input. It is necessary under the formulas to indicate the symbol ^ position where there is an incorrect input.

(a+b) - (ab) (a+b - (ab ^ ^ 
  • Question about syntax analysis? Formulas - only 4 operators and brackets, or something else? - Sergiks
  • Yes, only these operators and brackets. - Maxim Demyanyuk
  • The task as it is formulated: two lines with formulas are given, and it is necessary for the second to show the places of differences with the first? - Sergiks
  • A formula is considered to be incorrect if it contains opening brackets for which there are no closing ones or vice versa, if there are closing brackets for which there are no opening brackets. In the line below, the “^” indicates the position of incorrect brackets for which there is no pair. - Maxim Demyanyuk
  • Positions can be defined by a variety of options. In the example above, you can close it like this: (a)+b - (a)-b . Where should be the closing bracket in the formula (a + b - a - b ? - Sergiks

3 answers 3

Just check the number of open and closed brackets. If the numbers do not match, then there is an error somewhere. But the function finds only one error.

The error display function is taken from @Sergiks.

 $('input').on('change', function() { var value = this.value; var opened = 0; var last = 0; for(var i=0, sp=value.split(""); i < sp.length; i++) { if(sp[i] == "(") { last = i; opened++; } if(sp[i] == ")"){ last = i; opened--; } if(opened < 0) { show_error(last); break; } } if(opened != 0) { show_error(last); } }); function show_error(sym_no) { alert(sym_no); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input> 

  • Probably, here it is necessary to minimize the string to check for errors like (ab) , a - () b , ab + - () - Sergiks
  • @Sergiks and mc says only about brackets - lampa

As regards the placement of pointers under the line, it is easily solved by relative / absolute positioning. <span>^</span> inserted in the line in front of the desired place and moves down to the line height.

Suppose you somehow learned that errors are in positions 14 and 6 (the order is not important):

 placeMarks(14,6); function placeMarks() { var el = document.getElementById("code") ,html = el.innerText.trim() ,insert = '<span class="here">^</span>' ,places = Array.prototype.slice.call(arguments).sort((a,b)=>ab) ,pieces = [] ,i ,j = 0 ; for(i=0; i<places.length;i++) { pieces.push( html.slice(j,places[i])); j = places[i]; } pieces.push( html.slice(j)); el.innerHTML = pieces.join(insert); } 
 #code {font-size:16px;font-family:Courier New, monospace;position:relative} .here { position:absolute; top: 16px; } 
 <div id="code">a+b+c ) * (ab /(ac)</div> 

    You need to add an element containing ^ with absolute position and set it to some coordinates. I understand the question so much - how to calculate these coordinates.

    The solution is shamanic: we create 2 spans with a font similar to where your formulas are written. In the 1st span, we add a character through <BR> - the same number on which line you have a place to which you need to specify. In the 2nd span we add the last line before the character - which you want to point to. And then get the coordinates like this:

     var coordinates = { x: span2.offsetWidth, y: span1.offsetHeight }; 

    I did not see any other way to determine the coordinates of a symbol.