
- 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
Finding greatest digit by recursion - 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
Following is the code −
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
Following is the output in the console −
6
- Related Articles
- Finding difference of greatest and the smallest digit in a number - JavaScript
- JavaScript Recursion finding the smallest number?
- Greatest digit of a number in JavaScript
- Finding smallest number using recursion in JavaScript
- Finding product of an array using recursion in JavaScript
- Finding the nth digit of natural numbers JavaScript
- Finding a greatest number in a nested array in JavaScript
- Finding nth digit of natural numbers sequence in JavaScript
- Finding sequential digit numbers within a range in JavaScript
- Calculating factorial by recursion in JavaScript
- Determine the greatest four-digit number which is exactly divisible by 10,12,16.?
- Make the greatest and the smallest $4$-digit numbers by using any digit twice: $1,\ 8,\ 7$.
- Find the product of the greatest 5 digit number and greatest 4 digital number by using the distributive property.
- Determine the greatest 3 digit number exactly divisible by 8, 10, and 12.
- Which is the greatest 5 -digit number?

Advertisements