I feel stupid for asking but why is this returning true when it should be false?
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
Return true if and only if the given array A is monotonic.
var isMonotonic = function(A) {
let ascendOrder = A.sort((a,b) => a - b);
console.log(ascendOrder);
let descendOrder = A.sort((a,b) => b - a);
console.log(descendOrder);
if (A !== ascendOrder && A !== descendOrder) {
return false;
} else {
return true;
}
}
console.log(isMonotonic([1, 3, 2])); // false