726 B
726 B
title, tags
| title | tags |
|---|---|
| isSorted | array,intermediate |
Returns 1 if an array of numbers is sorted in ascending order, -1 if it is sorted in descending order or 0 if it is not sorted.
- Calculate the ordering
directionfor every pair of adjacent array elements. - Return
0if thedirectionchanges or thedirectionsign if the last element is reached.
const isSorted = arr => {
let direction = 0;
for (let i = 1; i < arr.length; i++) {
if (!direction) direction = arr[i] - arr[i - 1];
else if ((arr[i] - arr[i - 1]) * direction < 0) return 0;
}
return Math.sign(direction);
};
isSorted([0, 1, 2, 2]); // 1
isSorted([4, 3, 2]); // -1
isSorted([4, 3, 5]); // 0
isSorted([4]); // 0