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