

- 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
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 Questions & Answers
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Greatest digit of a number in JavaScript
- JavaScript Recursion finding the smallest number?
- Finding smallest number using recursion in JavaScript
- Finding a greatest number in a nested array in JavaScript
- Finding the nth digit of natural numbers JavaScript
- Calculating factorial by recursion in JavaScript
- Finding product of an array using recursion in JavaScript
- Finding nth digit of natural numbers sequence in JavaScript
- Finding sequential digit numbers within a range in JavaScript
- Greatest number divisible by n within a bound in JavaScript
- Is the digit divisible by the previous digit of the number in JavaScript
- Finding the group with largest elements with same digit sum in JavaScript
- Finding the largest 5 digit number within the input number using JavaScript
- Greatest Sum Divisible by Three in C++
Advertisements