Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda