Rotate number to form the maximum number using JavaScript



Problem

We are required to write a JavaScript function that takes in a number n. Our function is required to return the maximum value by rearranging its digits.

Example

Following is the code −

const num = 124; const rotateToMax = n => {    n = n       .toString()       .split('')       .map(el => +el);       n.sort((a, b) =>       return b - a;    });    return n    .join(''); }; console.log(rotateToMax(num));

Output

421
Updated on: 2021-04-21T07:00:42+05:30

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements