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

121
Views
Find first value in array with set num difference

Given a sorted array, I am looking to find the first value in the array where the next value has an increase/difference of >=1000. In the below array, my goal is to return the value 11710, because the next value in array (13271) has a difference of >= 1000.

Sample Array

const array = [11006, 11256, 11310, 11710, 13271, 327027, 327027]

Current Code // not returning correct value

const found = array.find((element, index) => {
   console.log('element', element),
   console.log('array[index + 1]', array[index + 1])
   return Math.abs(element, array[index + 1]) >= 1000
})
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to find the difference, so you should use -. Math.abs does not accept multiple arguments.

const array = [11006, 11256, 11310, 11710, 13271, 327027, 327027]
const found = array.find((element, index) => {
   return Math.abs(element - array[index + 1]) >= 1000
})
console.log(found);

about 4 years ago · Juan Pablo Isaza Report

0

The array is sorted. So you can be sure that the next element is always greater than the previous one. So you don't need to call the abs function at all.

const array = [11006, 11256, 11310, 11710, 13271, 327027, 327027];
const found = array.find((element, index) => { 
  return array[index + 1] - element >= 1000;
});
console.log(found);

This should be enough.

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!