最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Validate if the last value of array is greater than previous value - Stack Overflow

programmeradmin0浏览0评论

I have an array with value, [0,3,4,6,0], How do I validate if the before values is less than the after values?

var array = [0,3,4,6,0];
for(var i = 0; i < array.length; i++){
   //if the value of 0 >,3 >,4, > 6
   //return false;
}

I need to require the user to enter an ordered sequence of numbers like 5, 4, 3, 2, 1. Hence, I need to validate if the enter is not in an ordered sequence.

I have an array with value, [0,3,4,6,0], How do I validate if the before values is less than the after values?

var array = [0,3,4,6,0];
for(var i = 0; i < array.length; i++){
   //if the value of 0 >,3 >,4, > 6
   //return false;
}

I need to require the user to enter an ordered sequence of numbers like 5, 4, 3, 2, 1. Hence, I need to validate if the enter is not in an ordered sequence.

Share Improve this question edited Sep 10, 2016 at 16:38 Maher Abuthraa 17.9k12 gold badges86 silver badges105 bronze badges asked Sep 10, 2016 at 15:04 Echoing Throughout Tel NumaraEchoing Throughout Tel Numara 3532 gold badges8 silver badges18 bronze badges 4
  • You could find helpful this: check if a list is sorted in JS – Manu Artero Commented Sep 10, 2016 at 15:08
  • Check this stackoverflow./questions/29641569/… – mauriblint Commented Sep 10, 2016 at 15:09
  • You can use Array#every jsfiddle/r771nqcb – Jose Hermosilla Rodrigo Commented Sep 10, 2016 at 15:20
  • One line solution, if the array is not too large: (array.toString() == array.slice(0).sort().reverse()) – puritys Commented Sep 10, 2016 at 15:44
Add a ment  | 

4 Answers 4

Reset to default 4

A possible solution in ES5 using Array#every

function customValidate(array) {
  var length = array.length;
  return array.every(function(value, index) {
    var nextIndex = index + 1;
    return nextIndex < length ? value <= array[nextIndex] : true;
  });
}
console.log(customValidate([1, 2, 3, 4, 5]));
console.log(customValidate([5, 4, 3, 2, 1]));
console.log(customValidate([0, 0, 0, 4, 5]));
console.log(customValidate([0, 0, 0, 2, 1]));

Iterate all the array, expect true until you reach a false, where you can break out of the loop.

function ordered(array) {
  var isOk = true; // Set to true initially

  for (var i = 0; i < array.length - 1; i++) {
    if (array[i] > array[i + 1]) {
      // If something doesn't match, we're done, the list isn't in order
      isOk = false;
      break;
    }
  }
  document.write(isOk + "<br />");
}

ordered([]);
ordered([0, 0, 0, 1]);
ordered([1, 0]);
ordered([0, 0, 0, 1, 3, 4, 5]);
ordered([5, 0, 4, 1, 3, 4]);

        function inOrder(array){

            for(var i = 0; i < array.length - 1; i++){

                if((array[i] < array[i+1]))
                    return false;

            }
            return true;
        }

What you can do is to loop the array and pare adjacent elements like this:

var isOk = false;

for (var i = 0; i < array.length - 1; i++) {
    if (array[i] > array[i+1]) {
        isOk = true;
        break;
    }
}

Thus flag isOk will ensure that the whole array is sorted in descending order.

发布评论

评论列表(0)

  1. 暂无评论