在不使用 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP