找出 JavaScript 中數字的最大位數


我們需要編寫一個 JavaScript 遞迴函式,接收一個數字並返回該數字中的最大位。

例如:如果數字是 45654356

那麼返回值應該是 6

示例

其程式碼為 −

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));

輸出

控制檯中的輸出將為 −

6

更新於: 17-Oct-2020

485 次瀏覽

開啟你的 職業生涯

完成此課程獲得認證

開始
廣告
© . All rights reserved.