We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

My favorite solution although it might take awhile to compile with different constraints.

I wonder if there's a way to "clean up" my solution, which uses the lengths of both arrays as constraints.

function getTotalX(a, b) {

let maxIntA = Math.max(...a);
let minIntB = Math.min(...b);
let intsBetweenArrays = [];
let newArrA = [];
let finalArr = [];
// let isDivisible = Boolean;
if (maxIntA > minIntB) {
return 0;
} else {
for (let i = maxIntA; i <= minIntB; i++) {
intsBetweenArrays.push(i);
}
for (let i = 0; i < intsBetweenArrays.length; i++) {
if (a.every(num => intsBetweenArrays[i] % num === 0)) {
newArrA.push(intsBetweenArrays[i]);
}
}
for (let i = 0; i < newArrA.length; i++) {
if (b.every(num => num % newArrA[i] === 0)) {
finalArr.push(newArrA[i]);
}
}
}
return finalArr.length;

## Between Two Sets

You are viewing a single comment's thread. Return to all comments →

My javascript solution:Thats one of the most elegant solutions I have ever seen but can you explain to a novice what (a.every(int => (x % int == 0))) means?

It means this: For every value (

`int`

) in the array (`a`

), is`x`

evenly divisible by that value?For Javascript, [].every(function()) checks if all elements in an array pass a test which is the parameterized function as condition.

This is brilliant! Thanks!

This might be a silly question but why

`x <= 100`

?this is based on the constraints of the problem. we are told that each value in both arrays are between 1 < x <= 100

ah yes, thanks. I am not use to seeing the constraints accounted for in the code like that.

You're right, why x <= 100? Excution time may be reduced using max = Math.max(...b) instead of 100 ...

for x > max => b.every(int => (int % x == 0)) return false

My favorite solution although it might take awhile to compile with different constraints.

I wonder if there's a way to "clean up" my solution, which uses the lengths of both arrays as constraints.

function getTotalX(a, b) {

}