diff --git a/snippets/isSorted.md b/snippets/isSorted.md index fc29d3810..cd0a6a7fe 100644 --- a/snippets/isSorted.md +++ b/snippets/isSorted.md @@ -14,7 +14,7 @@ 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; + if (i === arr.length - 1) return !direction ? 0 : direction / Math.abs(direction); else if ((val - arr[i + 1]) * direction > 0) return 0; } }; @@ -24,4 +24,4 @@ const isSorted = arr => { isSorted([0, 1, 2, 2]); // 1 isSorted([4, 3, 2]); // -1 isSorted([4, 3, 5]); // 0 -``` \ No newline at end of file +```