숫자를 받는 JavaScript 함수를 작성해야 합니다. 그런 다음 함수는 숫자의 두 자리 사이에 존재하는 가장 큰 차이를 반환해야 합니다.
즉, 함수는 단순히 존재하는 가장 큰 숫자와 가장 작은 숫자 사이의 차이를 반환해야 합니다.
예:
If the number is 654646, Then the smallest digit here is 4 and the greatest is 6 Hence, our output should be 2
예시
이에 대한 코드는 -
const num = 654646;
const maxDifference = (num, min = Infinity, max = -Infinity) => {
if(num){
const digit = num % 10;
return maxDifference(Math.floor(num / 10), Math.min(digit, min),
Math.max(digit, max));
};
return max - min;
};
console.log(maxDifference(num)); 출력
콘솔의 출력 -
2