skip to Main Content

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


  1. function highestDigit(num) {
      let arr = num.toString().split('').map(Number);
      let maxDigit = Math.max(...arr);
      return maxDigit;
    }
    

    console.log(highestDigit(379)); // Output: 9

    Here we are spliting all digits into array and then checking for max from it.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search