I'm trying to solve a challenge - given an bitonic array, find the element index of its peak or trough. If the array is not bitonic, return a value of -1. I've set up my if-else statements as follows:
for (let i = 1; i < arr.length; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
let resultOne = arr.indexOf(arr[i]);
return resultOne;
}
else if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]){
let resultTwo = arr.indexOf(arr[i]);
return ResultTwo;
}
else {return -1;}
}
}
Every time it runs tests, the result is -1, which means the first two conditions are never met. Why, based on this, are these conditions never met? Thanks