
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Return the difference between the maximum & minimum number formed out of the number n in JavaScript
We have to write a function maximumDifference() that takes in a positive number n and returns the difference between the maximum number and the minimum number that can be formed out of the number n.
For example −
If the number n is 203,
The maximum number that can be formed from its digits will be 320
The minimum number that can be formed from its digits will be 23 (placing the zero at one’s place)
And the difference will be −
320-23 = 297
Therefore, the output should be 297
Let's write the code for this function −
Example
const digitDifference = num => { const asc = +String(num).split("").sort((a, b) => { return (+a) - (+b); }).join(""); const des = +String(num).split("").sort((a, b) => { return (+b) - (+a); }).join(""); return des - asc; }; console.log(digitDifference(203)); console.log(digitDifference(123)); console.log(digitDifference(546)); console.log(digitDifference(2354));
Output
The output in the console will be −
297 198 198 3087
- Related Articles
- Maximum difference between a number JavaScript
- Return the maximum number in each array using map JavaScript
- Queries to add, remove and return the difference of maximum and minimum in C++
- Queries to return the absolute difference between Lth smallest number and the R-th smallest number in C++
- Minimum and Maximum number of pairs in m teams of n people in C++
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number in C++ Program
- Finding out the Harshad number JavaScript
- Difference between a number and its reversed number JavaScript
- What is the minimum and maximum number of digits in the sum if we add any two 3 digit number
- Maximum and minimum of an array using minimum number of comparisons in C
- What is the difference between parseInt(string) and Number(string) in JavaScript?
- Find the Number of Subarrays whose Minimum and Maximum are Same using C++
- Rotate number to form the maximum number using JavaScript
- Find the difference between the smallest number of 7 digits and the largest number of digits.
- Golang Program to find the minimum and maximum number, using binary operations.

Advertisements