Skip to content

Commit 5ee943a

Browse files
committed
js7-write-a-program-to-find-the-largest-digit-in-a-number
1 parent 1291acd commit 5ee943a

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function largestDigit(num) {
2+
let maxDigit = 0;
3+
while (num > 0) {
4+
let digit = num % 10;
5+
if (digit > maxDigit) {
6+
maxDigit = digit;
7+
}
8+
num = Math.floor(num / 10);
9+
}
10+
return maxDigit;
11+
}
12+
13+
console.log(largestDigit(58724)); // Output: 8
14+
console.log(largestDigit(9483)); // Output: 9

0 commit comments

Comments
 (0)