Created
October 21, 2019 09:46
-
-
Save daubattu/5cdc2f5d332cfa5ddbf5b5c187499981 to your computer and use it in GitHub Desktop.
[Hackerrank] Solution of Between Two Sets in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
i dont understand can you explain this logic
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
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.
I can explain it easily.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code needs explanations,can u explain pls