Skip to content

Commit 84de2b6

Browse files
Create JS-Code1
Thank you for contributing the JavaScript implementation for LCM of an array. It improves the project by adding a useful math utility method. Merging this PR now. Great work!
1 parent 4ba97b9 commit 84de2b6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

JS-Code1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
function gcd(a, b) {
3+
while (b !== 0) {
4+
let temp = b;
5+
b = a % b;
6+
a = temp;
7+
}
8+
return a;
9+
}
10+
11+
function lcm(a, b) {
12+
return (a * b) / gcd(a, b);
13+
}
14+
15+
// Function to find LCM of an array
16+
function lcmOfArray(arr) {
17+
let result = arr[0];
18+
for (let i = 1; i < arr.length; i++) {
19+
result = lcm(result, arr[i]);
20+
}
21+
return result;
22+
}
23+
24+
25+
const arr = [4, 6, 8, 12];
26+
console.log("LCM:", lcmOfArray(arr));

0 commit comments

Comments
 (0)