숫자는 −
일 때 공백 숫자입니다.-
최소 세 자리 숫자가 있어야 하며
-
첫 번째 숫자와 마지막 숫자를 합친 숫자로 정확히 나눌 수 있습니다.
예:
1053 is a gapful number because it has 4 digits and it is exactly divisible by 13. 135 is a gapful number because it has 3 digits and it is exactly divisible by 15.
우리의 임무는 입력으로 제공한 숫자에 가장 가까운 공백 숫자를 반환하는 프로그램을 작성하는 것입니다.
코드를 작성해 봅시다 -
const n = 134;
//receives a number string and returns a boolean
const isGapful = (numStr) => {
const int = parseInt(numStr);
return int % parseInt(numStr[0] + numStr[numStr.length - 1]) === 0;
};
//main function -- receives a number, returns a number
const nearestGapful = (num) => {
if(typeof num !== 'number'){
return -1;
}
if(num <= 100){
return 100;
}
let prev = num - 1, next = num + 1;
while(!isGapful(String(prev)) && !isGapful(String(next))){
prev--;
next++;
};
return isGapful(String(prev)) ? prev : next;
};
console.log(nearestGapful(n)); 콘솔의 출력은 다음과 같습니다. -
135