在不使用 JavaScript 中的程式庫函式的情況下將數字轉換成對應的字串


問題

我們需要編寫一個 JavaScript 函式,它接收一個數字 n,並將其轉換成對應的字串,而不使用內建函式 String() 或 toString(),也不使用字串連線。

示例

以下是程式碼 −

 線上演示

const num = 235456;
const convertToString = (num) => {
   let res = '';
   while(num){
      res = (num % 10) + res;
      num = Math.floor(num / 10);
   };
   return res;
};
console.log(convertToString(num));

輸出

以下是控制檯輸出 −

235456

更新於: 2021 年 4 月 17 日

304 次瀏覽

開啟你的職業生涯

完成課程獲取認證

開始學習
廣告
© . All rights reserved.