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

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

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

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