
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Advertisements