如何计算给定数字的位数?的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 教师闺蜜祝福语简短