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

162
Visualizações
Find array element that is true for ALL elements in the second array

I'm trying to find the smallest common multiple from one array by comparing it to the values in the other by using % num === 0. The answer should be 6, but because each number in the first array is true at least once, they all get returned. How do I find the value that is only true and never false?

let arr = [1, 2, 3]

function test(arr) {
    let x = [1, 2, 3, 4, 5, 6, 7, 8]
    for (let n of arr) {
        return x.filter(k => k % n === 0)
    }
}

console.log(test(arr))

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need loop over the x array and return the first element that gets divided by every value in arr.

let arr = [1, 2, 3];

function test(arr) {
  let x = [1, 2, 3, 4, 5, 6, 7, 8];
  for (let n of x) {
    if (arr.every((a) => n % a === 0)) {
      return n;
    }
  }
}

console.log(test(arr));

You can also simply the solution using Array.prototype.find.

const 
  arr = [1, 2, 3],
  test = (arr) =>
    [1, 2, 3, 4, 5, 6, 7, 8].find((n) => arr.every((a) => n % a === 0));

console.log(test(arr));

Note: If x is not sorted, then you will have to sort it first.

const arr = [1, 2, 3],
  test = (arr) =>
    [8, 7, 6, 5, 4, 3, 2, 1]
      .sort((a, b) => a - b)
      .find((n) => arr.every((a) => n % a === 0));

console.log(test(arr));

Update based on OP's comment

You can use Array.prototype.filter and Array.prototype.some.

const arr = [1, 2, 3],
  test = (arr) =>
    [1, 2, 3, 4, 5, 6, 7, 8].filter((n) => arr.some((a) => n / a === 2));

console.log(test(arr));

about 4 years ago · Juan Pablo Isaza Relatório

0

Filter and intersect

let arr = [1, 2, 3]
let x = [1, 2, 3, 4, 5, 6, 7, 8]

function test(arr) {
  let common = []
  arr.forEach(n => common.push(x.filter(k => k % n === 0)))
  return common.reduce((acc, cur) => acc.filter(e => cur.includes(e)));
}


console.log(test(arr))

about 4 years ago · Juan Pablo Isaza Relatório

0

If x is sorted can use find and every

let arr = [1, 2, 3]
let x = [1, 2, 3, 4, 5, 6, 7, 8, 12]

let res = x.find(n => arr.every(a => n%a === 0))
console.log(res)

if unsorted x

let arr = [1, 2, 3]
let x = [1, 12, 6, 4, 2, 7, 8]

let res = [...x].sort((a,b)=> a-b).find(n => arr.every(a => n%a === 0))

console.log(res)

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