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

127
Views
Encuentre el primer valor en la matriz con la diferencia numérica establecida

Dada una matriz ordenada, busco encontrar el primer valor en la matriz donde el siguiente valor tiene un aumento/diferencia de> = 1000. En la siguiente matriz, mi objetivo es devolver el valor 11710, porque el siguiente valor en la matriz (13271) tiene una diferencia de >= 1000.

Matriz de muestra

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

Código actual // no devuelve el valor correcto

 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

Debe encontrar la diferencia , por lo que debe usar - . Math.abs no acepta múltiples argumentos.

 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

La matriz está ordenada. Entonces puede estar seguro de que el siguiente elemento siempre es mayor que el anterior. Por lo tanto, no necesita llamar a la función abs en absoluto.

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

Esto debería ser suficiente.

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!