Need help with JavaScript function to detect ascending numbers

I have table rows that have a start number and an end number (input fields).

In the line, the line must be larger than the beginning.

From the entrance from left to right, from top to bottom, the numbers should be larger than the last.

So, there are 2 entries per line and 4 (for example) lines. Each number is greater than the last.

I am trying to verify this using this function

   var maxDepth = 0,
  didValidate = true;

// I assume this reads from left to right top to bottom as they are that way in the markup
$('.input-start-depth, .input-end-depth').each(function(i) {

  maxDepth = Math.max(maxDepth, parseFloat($(this).val(), 10));

  var isStart = ($(this).hasClass('input-start-depth'));
  var value = $(this).val();


  if (isStart && value > maxDepth) {
    didValidate = false;

    return false;
  };

  lastValue = value;

});

I rack my brains to make it work. Another important thing - the number of lines is dynamic, it can be 1 or 10,000 or any intermediate.

Basically, this means that if the depth of the beginning is greater than the maximum depth, it should fail.

But he checks the numbers when they should not be valid.

What am I doing wrong?

Greetings.

+3
2

, . . , , :

$('table#mytable tr').each(function() {
  var start = parseInt($(this).find('.input-start-length').val());
  var end   = parseInt($(this).find('.input-end-length').val());
  if(start > maxDepth) {
    didValidate = false;
    return false;
  }
  lastvalue = start;
});
+1

, , ,

$('table tbody tr').each(function(i) {


                var startDepth = parseFloat($(this).find('.input-start-depth').val(), 10);
                var endDepth = parseFloat($(this).find('.input-end-depth').val(), 10);


                var anyNans = isNaN(startDepth) || isNaN(endDepth);

                if (anyNans || startDepth > endDepth || startDepth < maxDepth) {
                    didValidate = false;
                    return false;
                };
                maxDepth = Math.max(maxDepth, endDepth);

            });
0

Source: https://habr.com/ru/post/1752752/


All Articles