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