👍🏻 2022년 2월 2일
<script>
const makeLCM = (n1, n2) => {
let num = Math.min(n1, n2);
let gcd = 1;
for (let i=1; i<=num; i++) {
if (n1 % i === 0 && n2 % i === 0) gcd = i;
}
return gcd * (n1 / gcd) * (n2 / gcd)
}
function solution(arr) {
var answer = 0;
let lcm;
if (arr.length === 1) return arr[0];
else if (arr.length === 2) return makeLCM(arr[0], arr[1]);
else {
arr.sort((a,b) => b-a);
lcm = makeLCM(arr[0], arr[1]);
for (let i=2; i<arr.length-1; i++) {
lcm = makeLCM(lcm, arr[i]);
}
}
answer = lcm;
return answer;
}
</script>