

- 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
Greatest digit of a number in JavaScript
We are required to write a JavaScript recursive function that takes in a number and returns the greatest digit in the number.
For example: If the number is 45654356
Then the return value should be 6
Example
The code for this will be −
const num = 45654356; const greatestDigit = (num = 0, greatest = 0) => { if(num){ const max = Math.max(num % 10, greatest); return greatestDigit(Math.floor(num / 10), max); }; return greatest; }; console.log(greatestDigit(num));
Output
The output in the console will be −
6
- Related Questions & Answers
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Finding greatest digit by recursion - JavaScript
- Square every digit of a number - JavaScript
- Digit sum upto a number of digits of a number in JavaScript
- Greatest number in a dynamically typed array in JavaScript
- Finding a greatest number in a nested array in JavaScript
- Greater possible digit difference of a number in JavaScript
- Squaring every digit of a number using split() in JavaScript
- Greatest number divisible by n within a bound in JavaScript
- Corner digit number difference - JavaScript
- Negative number digit sum in JavaScript
- Check whether sum of digit of a number is Palindrome - JavaScript
- Is the digit divisible by the previous digit of the number in JavaScript
- Greatest sum of average of partitions in JavaScript
- Rearranging digits to form the greatest number using JavaScript
Advertisements