Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
Check if there is a value in the array which has more than 10 difference with other values

I want to check if there is a value in the array which has more than 10 difference with other values:

console.log(check());

function check() {
    const array = [21, 21, 10];  
    return array.some((val, i, arr) => val > arr[0] + 10);
}

The above array should return true because there is at least one value in the array which has more than 10 difference, But as you see it returns false!!

How can I do this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could take the max and min value and check the difference.

function check(array) {
    return Math.max(...array) - Math.min(...array) > 10;
}

console.log(check([21, 21, 10]));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You are checking if any of the array items are bigger than the first item + 10, which is not true.

You can check every element with the minimum element though:

console.log(check());

function check() {
    const array = [21, 21, 10];
    const min = Math.min(...array);
    return array.some(val => val > min + 10);
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda