Echoing Throughout Tel Numara Echoing Throughout Tel Numara - 11 months ago 49
Javascript Question

Javascript: Validate if the last value of array is greater than previous value

I have an array with value of [0,3,4,6,0], how do i validate if the before values is less than 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;

Because I need to required the user to enter an ordered number like 5, 4,3,2,1 that is why I need to validate if the enter is not in a sequence

Answer Source

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

var isOk = false;

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

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