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

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

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

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 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