如何计算给定数字的位数?的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 六十岁大寿祝福语简短