Rearranging digits to form the greatest number using JavaScript


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

Updated on: 17-Apr-2021

513 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements