I want to show the highest value of digit.
But it returns the 1 index
Here is the code
function highestDigit(num) {
let temp = [];
let arr = temp.push(num)
while (arr > 0) {
Math.max(arr)
return arr
}
}
console.log(highestDigit(379)) // answer return 1
I am trying to check all the value. And the return the highest value of the all the digit.
2
Answers
console.log(highestDigit(379)); // Output: 9
Here we are spliting all digits into array and then checking for max from it.
function highestDigit(num) { let temp = []; let arr = temp.push(num);
let maxDigit = 0;
while (arr > 0) { const digit = arr % 10; // Extract the last digit maxDigit = Math.max(maxDigit, digit); // Update maxDigit if the current digit is greater arr = Math.floor(arr / 10); // Remove the last digit from arr }
return maxDigit; }
console.log(highestDigit(379)); // Output: 9