JavaScript中备用索引处的数字差异
我们需要编写一个JavaScript函数,该函数接受一个数字并返回偶数位和奇数位的数字之和的差。
示例
为此的代码将是-
const num = 123456;
const alternateDifference = (num, res = 0, ind = 0) => {
if(num){
if(ind % 2 === 0){
res += num % 10;
}else{
res -= num % 10;
};
return alternateDifference(Math.floor(num / 10), res, ++ind);
};
return Math.abs(res);
};
console.log(alternateDifference(num));输出结果
控制台中的输出-
3
热门推荐
10 传统新婚祝福语创意简短
11 给长辈祝福语简短大全
12 高考送考祝福语简短
13 孩子16岁祝福语简短
14 圣诞祝福语简短小学
15 新年祝福语简短给孩子
16 简短霸气的考试祝福语
17 女神节日简短祝福语大全
18 合字的祝福语简短