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

160
Views
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 answers
Answer question

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 Report

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 Report

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