Rearranging digits to form the greatest number using JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in one positive three-digit integer and rearranges its digits to get the maximum possible number.

Example

Following is the code −

 Live Demo

const num = 149;
const maxRedigit = function(num) {
   if(num < 100 || num > 999)
      return null
   return +num
   .toString()
   .split('')
   .sort((a, b) => b - a)
   .join('')
};
console.log(maxRedigit(num));

Output

941
raja
Published on 17-Apr-2021 12:46:23
Advertisements