如何计算给定数字的位数?JavaScript
此处的要求很简单,我们需要编写一个 JavaScript 函数,获取一个数字并返回其位数。
例如 −
The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is 3
让我们编写此函数的代码 −
示例
const num = 2353454;
const digits = (num, count = 0) => {
if(num){
return digits(Math.floor(num / 10), ++count);
};
return count;
};
console.log(digits(num));
console.log(digits(123456));
console.log(digits(53453));
console.log(digits(5334534534));输出
控制台中的输出为 −
7 6 5 10
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP