836 B
836 B
title, tags
| title | tags |
|---|---|
| isSorted | array,intermediate |
Returns 1 if the array is sorted in ascending order, -1 if it is sorted in descending order or 0 if it is not sorted.
- Calculate the ordering
directionfor the first two elements. - Use
Object.entries()to loop over array objects and compare them in pairs. - Return
0if thedirectionchanges or thedirectionif the last element is reached.
const isSorted = arr => {
let direction = -(arr[0] - arr[1]);
for (let [i, val] of arr.entries()) {
direction = !direction ? -(arr[i - 1] - arr[i]) : direction;
if (i === arr.length - 1) return !direction ? 0 : direction / Math.abs(direction);
else if ((val - arr[i + 1]) * direction > 0) return 0;
}
};
isSorted([0, 1, 2, 2]); // 1
isSorted([4, 3, 2]); // -1
isSorted([4, 3, 5]); // 0