Program to check if a given array is sorted in ascending or descending order in Javascript


Below program helps you to check if a given array is sorted in any order or not

var numArray = [1,2,33,10,4];

var isDesc = true;
var isAsc = true;

for (let i=0, l=numArray.length-1; i<l; i++){
   isDesc = isDesc && (numArray[i] > numArray[i+1]);
   isAsc = isAsc && (numArray[i] < numArray[i+1]);
}

if (isAsc)
{
  console.log('Array is in Ascending order');
}
else if (isDesc) 
{
  console.log('Array is in Descending order');
}
else
{
  console.log('Array is not Sorted in any order');
}

Try checking out the result https://onecompiler.com/javascript/3xme4ugms with your own array.