Home > front end >  Returning index of an Array when it stops increasing and begins decreasing
Returning index of an Array when it stops increasing and begins decreasing

Time:02-10

having a problem when running two tests using this code, test 1 returns the whole array, test2 returns the appropriate index. (the point in the array in which it stops decreasing or increasing and starts the reverse) if there is only one sequence it should return -1

the test cases are Input: [-4, -2, 9, 10] Output: -1 Input: [5, 4, 3, 2, 10, 11] Output: 3

for (i = 0; i < arr.length; i  ) {
    while (arr[i] < arr[i   1]) {
      i  ;
      if (arr[i] < arr[i - 1]) {
        return -1
      }
      else if (arr[i] > arr[i   1]) {
        return i
      }
    } while (arr[i] > arr[i   1]) {
      i  ;
      if (arr[i] > arr[i - 1]) {
        return -1
      } else if (arr[i] < arr[i   1]) {
        return i
      }
    }
  }
  return arr;

}

CodePudding user response:

In the solution below, the isHomogeneous() method returns true if the content of the array is ever-increasing or ever-decreasing. The isIncreasing() method returns true if the array content is constantly decreasing. The isDecreasing() method returns true if the array content is ever-increasing. The getAscendingIndex() method returns the index at which the contents of the array begin to decrease. The getDescendingIndex() method returns the index at which the contents of the array begin to decrease. The application() method contains application code that executes other methods. If the content of the array is not homogeneous (continuously increasing or decreasing), the first index value at which the values start to increase in an array that starts with decreasing values is obtained using the getDescendingIndex() method.

/* Returns true if the array is descending to the elements (homogeneous). */
function isIncreasing(array) {
  for(let i = 0 ; i < array.length ;   i)
    if(array[i] < array[i   1])
      return false;
  return true;
}

/* Returns true if the array is incremental (homogeneous) to the elements. */
function isDecreasing(array) {
  for(let i = 0 ; i < array.length ;   i)
    if(array[i] > array[i   1])
      return false;
  return true;
}

/* Returns true if the array content is ever-increasing or ever-decreasing. */
function isHomogeneous(array) {
  return isIncreasing(array) || isDecreasing(array);
}

/* return the descending index in the array starting with increasing. */
function getAscendingIndex(array) {
  for(let i = 0 ; i < array.length ;   i)
    if(array[i] > array[i   1])
      return i;
  return -1;
}

/* return increasing index in array starting with decreasing. */
function getDescendingIndex(array) {
  for(let i = 0 ; i < array.length ;   i)
    if(array[i] < array[i   1])
      return i;
  return -1;
}

/* demo */
function application() {
  let firstArray = [-4, -2, 9, 10];
  let secondArray = [5, 4, 3, 2, 10, 11];
  
  console.log(`Increasing: ${(isIncreasing(firstArray) == true) ? "true" : "false"}`);
  console.log(`Decreasing: ${(isDecreasing(firstArray) == true) ? "true" : "false"}`);
  console.log(`First Array Index: ${getAscendingIndex(firstArray)}`);
  
 if(!isHomogeneous(secondArray) && getAscendingIndex(secondArray) != -1) {
    console.log(`Second Array Index: ${getDescendingIndex(secondArray)}`);
  }
}

application();

  •  Tags:  
  • Related