
- 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
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 Articles
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Finding greatest digit by recursion - JavaScript
- Which is the greatest 5 -digit number?
- Find the greatest five-digit number, which is a perfect square.
- Find the greatest four digit number which is a perfect square.
- Find the product of the greatest 5 digit number and greatest 4 digital number by using the distributive property.
- Square every digit of a number - JavaScript
- Digit sum upto a number of digits of a number 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
- Greatest number in a dynamically typed array in JavaScript
- Form the greatest 5-digit number using three different digits.
- Finding a greatest number in a nested array in JavaScript
- Greater possible digit difference of a number in JavaScript
- Which number is greater, the least $9$ digit number or the number which is $2$ more than the greatest $8$ digit number?
- Write the greatest 4-digit number and express it in terms of its prime factors.

Advertisements