JavaScript 数组中存在的数字和字符串数字之间的区别
问题
我们需要编写一个JavaScript函数,该函数接受整数的数字和字符串表示的混合数组。
我们的函数应该将所有字符串整数相加,然后从非字符串整数的总和中减去它。
示例
以下是代码-
const arr = [5, 2, '4', '7', '4', 2, 7, 9];
const integerDifference = (arr = []) => {
   let res = 0;
   for(let i = 0; i < arr.length; i++){
      const el = arr[i];
      if(typeof el === 'number'){
         res += el;
      }else if(typeof el === 'string' && +el){
         res -= (+el);
      };
   };
   return res;
};
console.log(integerDifference(arr));输出结果以下是控制台输出-
10