我们需要编写一个JavaScript函数,该函数接受一个数字并返回偶数位和奇数位的数字之和的差
让我们写代码-
const num = 345336;
const evenOddDifference = (num, res = 0, ind = 0) => {
if(num){
if(ind % 2 === 0){
res += num % 10;
}else{
res -= num % 10;
};
};
return Math.abs(res);
};
console.log(evenOddDifference(num));输出结果
以下是控制台中的输出-
2