Check if an array is descending, ascending or not sorted?

Viewed 19093

I'm just beginning with programming using javascript and I need to practice some questions to get EXP with the logic of code build. I got this question for homework but I can't make it work for some reason, even though it seems "logic" to me. Check if an array is descending, ascending or not sorted using loops.

I'm just a noob so please try and help me figure this out as I only got to loops in my studies (: this is the code I wrote:

        var array = [1, 2, 3, 7 ];
        var d = 0;
        var c =0 ;
        var b = 1;
        var a = 0;
        for (var i = 1; i <= array.length; i++)
            {
                if (array[c]<array[b] && a!== -1  ){
                   d = -1;
                   c =c+1;
                   b = b+1;
                   if(c==array.length){
                    console.log("asc");
                     break;
                   }else{
                     continue;
                }

                } else if (array[c]>array[b] && d!==-1 ){
                           a = -1;
                           d= d+1;
                           b = b+1;
                    if(i=array.length){
                    console.log("dsc");
                    break;
               }else{continue;}

               } else{

                    console.log("unsorted array");
                    break;
                }

            }
9 Answers
function isAscending(arr = []) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i + 1] <= arr[i]) {
      return false;
    }
  }
  return true;
}
function isAscending(arr) { 
  return arr
    .slice(1)
    .every((num,i) => num >= arr[i]); 
}

console.log(isAscending([1,2,3])); // true
console.log(isAscending([12,38,25])); // false
console.log(isAscending([103,398,52,629])); // false
  1. arr.slice(1) --> allows us to start iteration at index 1 instead of 0
  2. we'll iterate over "arr" with "every" & compare the current "num" against the previous "num" with "arr[i]"

Actually we may do even more by creating an Array method like natureOf which can tell us more about the nature of the array than just ascending, descendig or flat. Array.natureOf() shall give us a value between -1 and 1. If it is -1 the array is fully descending and 1 would mean fully ascending of course. Any value inbetween would give us the inclination of the array. As you would guess 0 would mean totally random or flat. (it's fairly easy to insert the logic to distinguish random from flat if that's also needed)

Array.natureOf = function(a){
                   var nature = a.reduce((n,e,i,a) => i && a[i-1] !== e ? a[i-1] < e ? (n.asc++, n)
                                                                                     : (n.dsc++, n)
                                                                        : n, {asc:0, dsc:0});
                   return (nature.asc - nature.dsc) / (a.length-1);
                 };
var arr = [1,2,3,4,5,6,7,8,9,10,7,11,13,14,15],
    brr = Array.from({length:2000000}, _ => ~~(Math.random()*1000000000));

console.log(`The nature of "arr" array is ${Array.natureOf(arr)}`);
console.log(`The nature of "brr" array is ${Array.natureOf(brr)}`);

  console.log(checkSort([1, 2, 3, 3, 4, 5]));
  console.log(checkSort([5, 4, 3, 2, 1, 1]));
  console.log(checkSort([2, 5, 8, 9, 4, 6]));

  function checkSort(arr){

    var isDescending, isAscending;
    isDescending = isAscending = true;

    const len = arr.length - 1;

    for (var index = 0 ; index < len ; index++){
      if(isAscending)
      isAscending = arr[index] <= arr[index + 1];// '<=' so as to check for same elements

      if(isDescending)
      isDescending = arr[index] >= arr[index + 1];//'<=' so as to check for same elements
    }

    var result = "Array is ";
    
    if (isAscending)
    return result.concat("sorted in ascending order");
    
    if (isDescending)
    return result.concat("sorted in descending order");
    
    return result.concat("not sorted");
Related