如何计算给定数字的位数?的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
热门推荐
10 五二零朋友祝福语大全简短
11 打游戏通关祝福语简短
12 苦难的成语祝福语简短
13 盛夏的温馨祝福语简短
14 贺卡祝福语情侣搞笑简短
15 送师姐的祝福语简短
16 档案调动孩子祝福语简短
17 叔叔生日祝福语简短搞笑
18 韩语贺卡祝福语简短版