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

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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