
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Check if an array is descending, ascending or not sorted in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. The function should check whether the numbers in the array are in increasing order, or in decreasing order or in no specific order.
If the array contains only one element then we should return a message saying not enough elements.
And if the array has all the elements equal, we should return a message saying all elements are equal.
Example
The code for this will be −
const arr1 = [7, 2, 1, 3, 2, 1]; const arr2 = [1, 1, 2, 3, 7, 7]; const determineOrder = arr => { if(arr.length < 2){ return 'not enough items'; }; let ascending = null; let nextArr = arr.slice(1); for(var i = 0; i < nextArr.length; i++) { if(nextArr[i] === arr[i]){ continue; }else if(ascending === null) { ascending = nextArr[i] > arr[i]; }else if (ascending !== nextArr[i] > arr[i]){ return 'unsorted'; }; } if(ascending === null){ return 'all items are equal'; }; return ascending ? 'ascending' : 'descending'; }; console.log(determineOrder(arr1)); console.log(determineOrder(arr2)); console.log(determineOrder([1, 1, 1, 1])); console.log(determineOrder([7, 2, 2, 1]));
Output
The output in the console −
unsorted ascending all items are equal descending
- Related Articles
- Program to check if an array is sorted or not (Iterative and Recursive) in C
- Check if a given array is pairwise sorted or not in C++
- Check if list is sorted or not in Python
- Check if an array is synchronized or not in C#
- Check if an array is read-only or not in C#
- JavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).
- Check if an array is sorted and rotated in Python
- Check if an array is sorted and rotated in C++
- Check if a binary tree is sorted levelwise or not in C++
- How to Check if an Array is Empty or Not in Java
- C Program to check if an Array is Palindrome or not
- Swift Program to check if an Array is Palindrome or not
- Check if a binary tree is sorted level-wise or not in C++
- Check if an Array has fixed size or not in C#
- Program to check if an Array is Palindrome or not using STL in C++

Advertisements