- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding difference of greatest and the smallest digit in a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns the difference between the greatest and the smallest digit present in it.
For example: If the number is 5464676, then the smallest digit here is 4 and the greatest is 7
Hence, our output should be 3
Example
Let’s write the code for this function −
const num = 44353456; const difference = (num, min = Infinity, max = -Infinity) => { if(num){ const digit = num % 10; return difference(Math.floor(num / 10), Math.min(digit, min), Math.max(digit, max)); }; return max - min; }; console.log(difference(num));
Output
The output in the console: −
3
- Related Articles
- Finding the greatest and smallest number in a space separated string of numbers using JavaScript
- Finding greatest digit by recursion - JavaScript
- Greatest digit of a number in JavaScript
- Greatest sum and smallest index difference in JavaScript
- JavaScript Recursion finding the smallest number?
- Finding the smallest fitting number in JavaScript
- Find the difference between the largest 3 digit number and the smallest 6 digit number.
- Find the difference between the largest 8-digit number and the smallest 6-digit number.
- Finding a greatest number in a nested array in JavaScript
- Write the smallest and the greatest 5-digit number using 0, 5, 1, 9, 6.
- Finding smallest number using recursion in JavaScript
- The difference between the greatest five-digit number and the greatest five-digit number with all different digits is:(a) 1000 (b) 12345(c) 1 (d) 1234
- Checking digit sum of smallest number in the array in JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- JavaScript - Find the smallest n digit number or greater

Advertisements