Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!