첫 번째와 두 번째 인수로 두 개의 숫자를 사용하는 JavaScript 함수를 작성해야 합니다. 이를 m과 n이라고 합니다.
첫 번째 숫자는 일반적으로 여러 자릿수를 가진 숫자이고 두 번째 숫자는 항상 첫 번째 숫자의 자릿수보다 작습니다.
함수는 곱이 가장 큰 m에서 n개의 연속된 숫자 그룹을 찾아야 합니다.
예를 들어 -
입력된 숫자가 -
인 경우const m = 65467586; const n = 3;
그러면 출력은 다음과 같아야 합니다. -
const output = 280;
7 * 5 * 8 =280이고 이 숫자에서 연속되는 최대 세 자리 곱이기 때문에
예시
다음은 코드입니다 -
const m = 65467586;
const n = 3;
const largestProductOfContinuousDigits = (m, n) => {
const str = String(m);
if(n > str.length){
return 0;
};
let max = -Infinity;
let temp = 1;
for(let i = 0; i < n; i++){
temp *= +(str[i]);
};
max = temp;
for(i = 0; i < str.length - n; i++){
temp = (temp / (+str[i])) * (+str[i + n]);
max = Math.max(temp, max);
};
return max;
}
console.log(largestProductOfContinuousDigits(m, n)); 출력
다음은 콘솔 출력입니다 -
280