

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Returning number with increasing digits. in JavaScript
Problem
We are required to write a JavaScript function that takes in a number n. Our function should return it with its digits in descending order. Essentially, we should rearrange the digits to create the highest possible number.
Example
Following is the code −
const num = 5423267; const arrangeInDescending = (num = 1) => { const str = String(num); const arr = str.split(''); arr.sort((a, b) => { return +b - +a; }); const newStr = arr.join(''); const res = Number(newStr); return res; }; console.log(arrangeInDescending(num));
Output
Following is the console output −
7654322
- Related Questions & Answers
- Returning number of digits in factorial of a number in JavaScript
- Monotone Increasing Digits in C++
- Just smaller number with monotone digits in JavaScript
- Returning the nth even number using JavaScript
- Returning only odd number from array in JavaScript
- Fetch Numbers with Even Number of Digits JavaScript
- Returning the expanded form of a number in JavaScript
- Total number of longest increasing sequences in JavaScript
- Finding product of Number digits in JavaScript
- Separating digits of a number in JavaScript
- Number Split into individual digits in JavaScript
- Sorting array of Number by increasing frequency JavaScript
- Largest number with prime digits in C++
- Find smallest number with given number of digits and sum of digits in C++
- Python Program to extracts elements from a list with digits in increasing order
Advertisements