forked from akshitagit/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhfc1.js
More file actions
43 lines (22 loc) · 705 Bytes
/
hfc1.js
File metadata and controls
43 lines (22 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// numbers = [2, 4, 6]
// numbers = [54, 24]
numbers = [15, 25, 35]
function hcf(num, numbers) {
let commonFactors = [];
let highestCommonFactor;
for (let i = 0; i < num; i++) {
for (let j = 1; j <= numbers[i]; j++) {
if (numbers[i] % j === 0) {
commonFactors.push(j)
}
}
}
let sortedCommonFactors = commonFactors.sort((a, b) => b - a);
for (let k = sortedCommonFactors.length; k > 0; k--) {
if (sortedCommonFactors[k] == sortedCommonFactors[k + (num - 1)]) {
highestCommonFactor = sortedCommonFactors[k];
}
}
return highestCommonFactor
}
console.log(hcf(3, numbers))