https://programmers.co.kr/learn/courses/30/lessons/12940?language=javascript
function calcDivisor(num) {
let divisor = [];
for (let i = 1; i <= num; i++) {
if (num % i === 0) {
divisor.push(i);
}
}
return divisor;
}
function solution(n, m) {
let answer = [];
// 약수 구하기
const nDivisor = calcDivisor(n); // [1, 3]
const mDivisor = calcDivisor(m); // [1, 2, 3, 4, 6, 12]
// 작은 수, 큰 수 구하기
let lowNumber = [];
let highNumber = [];
if (n <= m) {
lowNumber = nDivisor;
highNumber = mDivisor;
} else {
lowNumber = mDivisor;
highNumber = nDivisor;
}
// 최대공약수 구하기
let GCD = 0;
for (let i = lowNumber.length - 1; i >= 0; i--) {
const index = highNumber.indexOf(lowNumber[i]);
if (index !== -1) {
GCD = highNumber[index];
answer.push(GCD);
break;
}
}
// 최소공배수 구하기
const LCM = (n * m) / GCD;
answer.push(LCM);
return answer;
}