Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daubattu/5cdc2f5d332cfa5ddbf5b5c187499981 to your computer and use it in GitHub Desktop.
Save daubattu/5cdc2f5d332cfa5ddbf5b5c187499981 to your computer and use it in GitHub Desktop.
[Hackerrank] Solution of Between Two Sets in JavaScript
function getTotalX(a, b) {
// Write your code here
let result = 0;
let index = 1;
let cloneA = a.splice(1, a.length); // clone new array of a but not a[0]
while(a[0] * index <= b[0]) {
if(
cloneA.every(item => (a[0] * index) % item === 0)
&&
b.every(item => item % (a[0] * index) === 0)
) {
result++;
}
index++;
}
return result;
}
@jude92
Copy link

jude92 commented Jul 30, 2022

This code needs explanations,can u explain pls

@vinodm1609
Copy link

i dont understand can you explain this logic

@egstar
Copy link

egstar commented Aug 27, 2022

This code needs explanations,can u explain pls

i dont understand can you explain this logic

function getTotalX(a, b) {
    let count = 0
    let na = Math.max(...a)
    let mi = Math.min(...b)
    for(let i=na; i<=mi ;i++){ // Loop between Array[a](MAX) and Array[b](MIN)
        if((a.every(e => i % e === 0)) && b.every(e => e % i === 0)){ 
// Check if the i(INTEGER) is Factor of Array[a](ELEMENTS) & Array[b](ELEMENTS) are Factor of the i(INTEGER)
            count++ // add +1 for each found integer
        }
    }
     return count
}

the same code, with more clearance

@amaaz-arshad
Copy link

amaaz-arshad commented Oct 9, 2022

let count = 0
let na = Math.max(...a)
let mi = Math.min(...b)
for(let i=na; i<=mi ;i++){ // Loop between Arraya and Arrayb
if((a.every(e => i % e === 0)) && b.every(e => e % i === 0)){
// Check if the i(INTEGER) is Factor of Arraya & Arrayb are Factor of the i(INTEGER)
count++ // add +1 for each found integer
}
}
return count

Nice solution and easy to understand. Comments helped.

@ChanchalS7
Copy link

I can explain it easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment