Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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 −
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
Advertisements
