Thursday, 12 September 2013

Hoe to show largest digit from a number - javascript

Hoe to show largest digit from a number - javascript

I have this function that calculates the largest digit from a number:
function maxDigit(n){
if(n == 0){ return 0;}
else{
return Math.max(n%10, maxDigit(n/10));
}
}
console.log(maxDigit(16984));
and the returned value is 9.840000000000003
How can I modifiy this code in order to return just the value 9 ?

No comments:

Post a Comment