Digit Swapping function without converting it into a string.
/**
* Determines if a number is the largest possible number that can be obtained by swapping its digits.
* @param {number} num - The input number to check.
* @returns {boolean} Returns true if the input number is greater than or equal to the largest possible number obtained by swapping its digits; otherwise, returns false.
*/
function largestSwap(num) {
let originNum = num;
let swapNumber = 0;
while (num > 0) {
let digit = num % 10;
swapNumber = swapNumber * 10 + digit;
num = Math.floor(num / 10);
}
return originNum >= swapNumber;
}Last updated