I'm currently working on a code problem where I need to loop over the values of an array and determine whether they are in ascending order, descending order, or neither. The function I've written so far is not giving me the correct results, but I am determined to figure out what's wrong with it.
While I have seen another approach to solving this problem, I believe that my current logic is heading in the right direction. As a learner, I want to continue exploring my own solution before resorting to alternatives. Thank you for your help!
function ascDscArray(array) {
for (var i = 1; i < array.length - 1; i++) {
if (array[i - 1] < array[i]) return "yes, ascending";
if (array[i - 1] > array[i]) return "yes, descending";
else return "no";
}
}
Just a heads up: I'm trying to test this function with two example arrays: [15, 7, 3, -8]
and [4, 2, 30]
.