Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
How can you use .indexOf to locate the index of an array where the numbers stop increasing?

I'm taking in an array and need to return the index value where the numbers start to increase or decrease. The numbers in the array either increase then decrease [1, 3, 6, 4, 3](output = 2) decrease then increase [6, 4, 10, 12, 19](output = 1) or the same sequence [1, 3, 5, 7, 9](output = -1). For now I'm just focused on returning the index of an array that increase then decrease, then I think I can figure the other conditions out.

function ArrayChallenge(arr){
  
  for(let i = 0; i < arr.length; i++){
    if(arr[i]>arr[i+1]){
      console.log(arr.indexOf(arr[i]>arr[i+1]))
    }
  }
}

console.log(ArrayChallenge([1, 2, 4, 5, 7, 3]))

To me the code says, take in the argument for the parameter(arr), then the for loop will go through each index and compare i with i+1, if i is greater than i+1 that means the sequence is decreasing. If that's the case I'd like to console log the index where that's actually happening. For my example code above the output should be 5 because that's the index where the numbers start decreasing, but I'm getting -1 which means the element cannot be found. If someone knows where I'm going wrong and can point me in the right direction that would be great, I'm pretty sure I'm using the .indexOf method incorrectly, but I don't know why.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

"arr[i]>arr[i+1]" returns a bool "true". Your array does not contain any bool values so there is no matching index for it. Therefore it returns -1 because it can not find any matching element.

EDIT:

If you want to print out the "moment" when your values are decreasing you can try something like this:

console.log('Decreasing from index: ' + arr.indexOf(arr[i]) + ' to ' + arr.indexOf(arr[i + 1]))
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use return arr[i] instead of console.log(arr.indexOf(arr[i]>arr[i+1]))

function ArrayChallenge(arr){
  
  for(let i = 0; i < arr.length; i++){
    if(arr[i]>arr[i+1]){
      return arr[i]
    }
  }
}

console.log(ArrayChallenge([1, 2, 4, 5, 7, 3]))
output: 7
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use ES6 Classes concept to achieve the requirement you have.

Working Demo :

// Defining class using es6
class arrayChallenge {
  constructor(input_array) {
    this.input_array = input_array;
  }
  getIncreaseThenDecrease() {
    for(let i = 0; i < this.input_array.length; i++) {
        if(this.input_array[i]>this.input_array[i+1]) {
        return this.input_array.indexOf(this.input_array[i])
        }
    }
  }
  
  getdecreaseThenIncrease() {
    for(let i = 0; i < this.input_array.length; i++) {
        if(this.input_array[i] < this.input_array[i+1]) {
        return this.input_array.indexOf(this.input_array[i])
        }
    }
  }
}
// Making object with the help of the constructor
let arrayChallengeObject = new arrayChallenge([1, 2, 4, 5, 7, 3]);
console.log(arrayChallengeObject.getIncreaseThenDecrease());
console.log(arrayChallengeObject.getdecreaseThenIncrease());

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda